【发布时间】:2021-12-15 15:51:23
【问题描述】:
我在 Laravel 中使用我定义的路径。 但它给了我一个未定义的信息 我检查了所有的路线。 我什至清除了缓存。 这对我来说很不寻常,因为我的其他路径以相同的方式工作
这是我的身体
@extends('layouts.master')
@section('content')
@if(Session::has('info'))
<div class="bg-dark text-white">
<p>box:{{Session::get('info')}}</p>
</div>
@endif
<div>
<a class="btn btn-dark" href="{{route('admin.new-post')}}">New Post</a>
<br /><br />
@foreach($posts as $post)
<div class="text-center">
<div>
<h1>{{$post->title}}</h1>
<p>
{{$post->content}}
</p>
</div>
<a class="btn btn-dark" href="{{route('admin.edit-post',['id' => $post->id])}}">Edit Post</a>
<a class="btn btn-dark" href="{{route('admin.delete-post',['id' => $post->id])}}">Delete Post</a>
</div>
</div>
@endforeach
@endsection
这是我的路线
Route::group(['prefix'=>'admin'],function(){
Route::get('',[
'uses' => 'App\Http\Controllers\PostController@getAdminIndex',
'as' => 'admin'
]);
Route::get('new-post',[
'uses' => 'App\Http\Controllers\PostController@getAdminCreate',
'as' => 'admin.new-post'
]);
Route::post('create', [
'uses' => 'App\Http\Controllers\PostController@postAdminCreate',
'as' => 'admin.create-post'
]);
Route::get('edit-post/{id}',[
'uses' => 'App\Http\Controllers\PostController@getAdminEdit',
'as' => 'admin.edit-post'
]);
Route::post('edit',[
'uses' => 'App\Http\Controllers\PostController@postAdminUpdate',
'as' => 'admin.update'
]);
Route::get('delete-post/{id}', 'App\Http\Controllers\PostController@postDelete')
->name('admin.delete-post');
});
这是我的控制,但我得到了找不到路径的错误。 所有代码中的一切都很简单。 代码中没有歧义。 谢谢你为我付出的努力
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use Illuminate\Session\Store;
use Illuminate\Database\Eloquent\Model;
class PostController extends Controller
{
public function getIndex(Store $session){
$posts = Post::all();
return view("blog.home",['posts' => $posts]);
}
public function getAdminIndex(Store $session){
$posts = Post::all();
return view("admin.index",['posts' => $posts]);
}
public function getPost(Store $session, $id){
$post = Post::find($id);
return view("blog.post",['post' => $post]);
}
public function getAdminCreate(Store $session){
return view('admin.new-post');
}
public function getAdminEdit(Store $session, $id){
$post = Post::find($id);
return view("admin.edit-post",['post' => $post , 'postId' => $id]);
}
public function postAdminCreate(Store $session, Request $request){
$this->validate($request,[
'title' => 'Required | min:5',
'content' => 'Required | min:10'
]);
$post = new Post([
'title' => $request->input('title'),
'content' => $request->input('content')
]);
$post->save();
return redirect()->route('admin')->with('info','post created, title is :'.
$request->input('title'));
}
public function postAdminUpdate(Store $session, Request $request){
$this->validate($request,[
'title' => 'Required | min:5',
'content' => 'Required | min:10'
]);
$post = Post::find($request->input('id'));
$post->title = $request->input('title');
$post->content = $request->input('content');
$post->save();
return redirect()
->route('admin')
->with('info','post edited, title is :'. $request
->input('title'));
}
public function postDelete($id){
$post = Post::find($id);
$post->delete();
return redirect()->route('admin.index')->with('info', 'Delete your post');
}
}
```
this my Exception
> Symfony\Component\Routing\Exception\RouteNotFoundException
Route [admin.delete-post] not defined. (View: C:\xampp\htdocs\my-laravel\resources\views\admin\index.blade.php)`
thank you of all
【问题讨论】:
-
这是什么版本的 laravel?
-
您是否尝试过使用
as关键字?就像你的其他路线一样? -
laravel-8 。我为所有路线使用路线名称
-
请添加有关您收到的错误消息的详细信息,
an undefined message不是很有帮助。另外,添加您的控制器,以便我们可以排除缺少的操作方法。 -
嗨。我还在上面的代码中添加了一个控制器。代码非常简单流畅