Laravel 5.2 使用资源进行 CRUD 操作时路由不工作
Laravel 5.2 Route not working when using resource for CRUD operation
我正在尝试熟悉 Laravel 框架。我一直在学习教程,但在路由部分遇到了困难。我有一个简单的博客项目,试图创建和编辑博客 post。我已经有 2 个其他对象的 CRUD 操作正常工作 - 其中 1 个是在路由中设置资源的,就像我尝试对这个对象做的一样。尝试对以下内容进行 post 时:
<form class="form-horizontal" role="form" method="POST"
action="{{ route('admin.post.store') }}">
我返回错误 "Route [admin.post.store] not defined. (View: /home/vagrant/Code/test-dev/resources/views/admin/post/create.blade.php)"
现在我明白这是一个问题,它能够找到默认存储在我的模型 post 对象的基 class 中的存储过程。我使用 artisan 和 migration 来创建这些对象并自动将它们绑定到数据库对象。 Post.php 模型对象 post 编辑如下:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $dates = ['published_at'];
protected $fillable = [
'title', 'subtitle', 'content_raw', 'page_image', 'meta_description',
'layout', 'is_draft', 'published_at',
];
//// other methods here that help pass slug and do DB relationship support
//// If someone thinks they are relevant I can addthem
}
这是 "routes" 文件夹中的 web.php 文件:
Route::get('/', function () {
return redirect('/blog');
});
Route::get('blog', 'BlogController@index');
Route::get('blog/{slug}', 'BlogController@showPost');
// Admin area
Route::get('admin', function () {
return redirect('/admin/post');
});
$router->group([
'namespace' => 'Admin',
'middleware' => 'auth',
], function () {
Route::resource('admin/post', 'PostController', ['except' => 'show']);
Route::resource('admin/tag', 'TagController', ['except' => 'show']);
Route::get('admin/upload', 'UploadController@index');
Route::post('admin/upload/file', 'UploadController@uploadFile');
Route::delete('admin/upload/file', 'UploadController@deleteFile');
Route::post('admin/upload/folder', 'UploadController@createFolder');
Route::delete('admin/upload/folder', 'UploadController@deleteFolder');
});
Auth::routes();
Route::get('/home', 'HomeController@index');
这是 PostController
<?php
namespace App\Http\Controllers\Admin;
use App\Jobs\PostFormFields;
use App\Http\Requests;
use App\Http\Requests\PostCreateRequest;
use App\Http\Requests\PostUpdateRequest;
use App\Http\Controllers\Controller;
use App\Post;
class PostController extends Controller
{
/**
* Display a listing of the posts.
*/
public function index()
{
return view('admin.post.index')
->withPosts(Post::all());
}
/**
* Show the new post form
*/
public function create()
{
$data = $this->dispatch(new PostFormFields());
return view('admin.post.create', $data);
}
/**
* Store a newly created Post
*
* @param PostCreateRequest $request
*/
public function store(PostCreateRequest $request)
{
$post = Post::create($request->postFillData());
$post->syncTags($request->get('tags', []));
return redirect()
->route('admin.post.index')
->withSuccess('New Post Successfully Created.');
}
/**
* Show the post edit form
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$data = $this->dispatch(new PostFormFields($id));
return view('admin.post.edit', $data);
}
/**
* Update the Post
*
* @param PostUpdateRequest $request
* @param int $id
*/
public function update(PostUpdateRequest $request, $id)
{
$post = Post::findOrFail($id);
$post->fill($request->postFillData());
$post->save();
$post->syncTags($request->get('tags', []));
if ($request->action === 'continue') {
return redirect()
->back()
->withSuccess('Post saved.');
}
return redirect()
->route('admin.post.index')
->withSuccess('Post saved.');
}
}
最后 - PostCreateRequest class:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Carbon\Carbon;
class PostCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required',
'subtitle' => 'required',
'content' => 'required',
'publish_date' => 'required',
'publish_time' => 'required',
'layout' => 'required',
];
}
/**
* Return the fields and values to create a new post from
*/
public function postFillData()
{
$published_at = new Carbon(
$this->publish_date.' '.$this->publish_time
);
return [
'title' => $this->title,
'subtitle' => $this->subtitle,
'page_image' => $this->page_image,
'content_raw' => $this->get('content'),
'meta_description' => $this->meta_description,
'is_draft' => (bool)$this->is_draft,
'published_at' => $published_at,
'layout' => $this->layout,
];
}
}
使用 Artisan returns 检查路由列表如下:
POST | admin/post | post.store | App\Http\Controllers\Admin\PostController@store | web,auth
GET|HEAD | admin/post | post.index | App\Http\Controllers\Admin\PostController@index | web,auth |
GET|HEAD | admin/post/create | post.create | App\Http\Controllers\Admin\PostController@create | web,auth |
PUT|PATCH | admin/post/{post} | post.update | App\Http\Controllers\Admin\PostController@update | web,auth |
DELETE | admin/post/{post} | post.destroy | App\Http\Controllers\Admin\PostController@destroy
我不明白的是为什么其他对象似乎在完全相同的条件下工作。 post 表单的完成方式略有不同,我认为这是导致问题的原因。我觉得还有其他东西需要配置或 "connected" 以确保 {{ route()}} 调用找到正确的 url.
非常感谢任何帮助。
通过查看您的路线列表,确认没有您在表格中使用的名为 admin.post.store
的路线。
所以要解决这个问题,您可以更改资源路由的名称。
示例来自 docs:
Route::resource('photo', 'PhotoController', ['names' => [
'create' => 'photo.build'
]]);
所以在你的情况下,它将是:
Route::resource('admin/post', 'PostController', ['except' => 'show', 'names' => [
'store' => 'admin.post.store'
]]);
像这样,你可以给路由定义更多的名字。
更新
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
Route::resource('post', 'PostController', ['except' => 'show']);
});
尝试更改和更新您的作曲家:
$router->group([
'namespace' => 'Admin',
'middleware' => 'auth',
], function () {
Route::resource('admin/post', 'PostController', ['except' => 'show']);
Route::resource('admin/tag', 'TagController', ['except' => 'show']);
Route::get('admin/upload', 'UploadController@index');
Route::post('admin/upload/file', 'UploadController@uploadFile');
Route::delete('admin/upload/file', 'UploadController@deleteFile');
Route::post('admin/upload/folder', 'UploadController@createFolder');
Route::delete('admin/upload/folder', 'UploadController@deleteFolder');
});
到
Route::group([
'namespace' => 'Admin',
'middleware' => 'auth',
], function () {
Route::get('admin/upload', 'UploadController@index');
Route::post('admin/upload/file', 'UploadController@uploadFile');
Route::delete('admin/upload/file', 'UploadController@deleteFile');
Route::post('admin/upload/folder', 'UploadController@createFolder');
Route::delete('admin/upload/folder', 'UploadController@deleteFolder');
Route::resource('admin/post', 'PostController', ['except' => 'show']);
Route::resource('admin/tag', 'TagController', ['except' => 'show']);
});
我正在尝试熟悉 Laravel 框架。我一直在学习教程,但在路由部分遇到了困难。我有一个简单的博客项目,试图创建和编辑博客 post。我已经有 2 个其他对象的 CRUD 操作正常工作 - 其中 1 个是在路由中设置资源的,就像我尝试对这个对象做的一样。尝试对以下内容进行 post 时:
<form class="form-horizontal" role="form" method="POST"
action="{{ route('admin.post.store') }}">
我返回错误 "Route [admin.post.store] not defined. (View: /home/vagrant/Code/test-dev/resources/views/admin/post/create.blade.php)"
现在我明白这是一个问题,它能够找到默认存储在我的模型 post 对象的基 class 中的存储过程。我使用 artisan 和 migration 来创建这些对象并自动将它们绑定到数据库对象。 Post.php 模型对象 post 编辑如下:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $dates = ['published_at'];
protected $fillable = [
'title', 'subtitle', 'content_raw', 'page_image', 'meta_description',
'layout', 'is_draft', 'published_at',
];
//// other methods here that help pass slug and do DB relationship support
//// If someone thinks they are relevant I can addthem
}
这是 "routes" 文件夹中的 web.php 文件:
Route::get('/', function () {
return redirect('/blog');
});
Route::get('blog', 'BlogController@index');
Route::get('blog/{slug}', 'BlogController@showPost');
// Admin area
Route::get('admin', function () {
return redirect('/admin/post');
});
$router->group([
'namespace' => 'Admin',
'middleware' => 'auth',
], function () {
Route::resource('admin/post', 'PostController', ['except' => 'show']);
Route::resource('admin/tag', 'TagController', ['except' => 'show']);
Route::get('admin/upload', 'UploadController@index');
Route::post('admin/upload/file', 'UploadController@uploadFile');
Route::delete('admin/upload/file', 'UploadController@deleteFile');
Route::post('admin/upload/folder', 'UploadController@createFolder');
Route::delete('admin/upload/folder', 'UploadController@deleteFolder');
});
Auth::routes();
Route::get('/home', 'HomeController@index');
这是 PostController
<?php
namespace App\Http\Controllers\Admin;
use App\Jobs\PostFormFields;
use App\Http\Requests;
use App\Http\Requests\PostCreateRequest;
use App\Http\Requests\PostUpdateRequest;
use App\Http\Controllers\Controller;
use App\Post;
class PostController extends Controller
{
/**
* Display a listing of the posts.
*/
public function index()
{
return view('admin.post.index')
->withPosts(Post::all());
}
/**
* Show the new post form
*/
public function create()
{
$data = $this->dispatch(new PostFormFields());
return view('admin.post.create', $data);
}
/**
* Store a newly created Post
*
* @param PostCreateRequest $request
*/
public function store(PostCreateRequest $request)
{
$post = Post::create($request->postFillData());
$post->syncTags($request->get('tags', []));
return redirect()
->route('admin.post.index')
->withSuccess('New Post Successfully Created.');
}
/**
* Show the post edit form
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$data = $this->dispatch(new PostFormFields($id));
return view('admin.post.edit', $data);
}
/**
* Update the Post
*
* @param PostUpdateRequest $request
* @param int $id
*/
public function update(PostUpdateRequest $request, $id)
{
$post = Post::findOrFail($id);
$post->fill($request->postFillData());
$post->save();
$post->syncTags($request->get('tags', []));
if ($request->action === 'continue') {
return redirect()
->back()
->withSuccess('Post saved.');
}
return redirect()
->route('admin.post.index')
->withSuccess('Post saved.');
}
}
最后 - PostCreateRequest class:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Carbon\Carbon;
class PostCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required',
'subtitle' => 'required',
'content' => 'required',
'publish_date' => 'required',
'publish_time' => 'required',
'layout' => 'required',
];
}
/**
* Return the fields and values to create a new post from
*/
public function postFillData()
{
$published_at = new Carbon(
$this->publish_date.' '.$this->publish_time
);
return [
'title' => $this->title,
'subtitle' => $this->subtitle,
'page_image' => $this->page_image,
'content_raw' => $this->get('content'),
'meta_description' => $this->meta_description,
'is_draft' => (bool)$this->is_draft,
'published_at' => $published_at,
'layout' => $this->layout,
];
}
}
使用 Artisan returns 检查路由列表如下:
POST | admin/post | post.store | App\Http\Controllers\Admin\PostController@store | web,auth
GET|HEAD | admin/post | post.index | App\Http\Controllers\Admin\PostController@index | web,auth |
GET|HEAD | admin/post/create | post.create | App\Http\Controllers\Admin\PostController@create | web,auth |
PUT|PATCH | admin/post/{post} | post.update | App\Http\Controllers\Admin\PostController@update | web,auth |
DELETE | admin/post/{post} | post.destroy | App\Http\Controllers\Admin\PostController@destroy
我不明白的是为什么其他对象似乎在完全相同的条件下工作。 post 表单的完成方式略有不同,我认为这是导致问题的原因。我觉得还有其他东西需要配置或 "connected" 以确保 {{ route()}} 调用找到正确的 url.
非常感谢任何帮助。
通过查看您的路线列表,确认没有您在表格中使用的名为 admin.post.store
的路线。
所以要解决这个问题,您可以更改资源路由的名称。
示例来自 docs:
Route::resource('photo', 'PhotoController', ['names' => [
'create' => 'photo.build'
]]);
所以在你的情况下,它将是:
Route::resource('admin/post', 'PostController', ['except' => 'show', 'names' => [
'store' => 'admin.post.store'
]]);
像这样,你可以给路由定义更多的名字。
更新
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
Route::resource('post', 'PostController', ['except' => 'show']);
});
尝试更改和更新您的作曲家:
$router->group([
'namespace' => 'Admin',
'middleware' => 'auth',
], function () {
Route::resource('admin/post', 'PostController', ['except' => 'show']);
Route::resource('admin/tag', 'TagController', ['except' => 'show']);
Route::get('admin/upload', 'UploadController@index');
Route::post('admin/upload/file', 'UploadController@uploadFile');
Route::delete('admin/upload/file', 'UploadController@deleteFile');
Route::post('admin/upload/folder', 'UploadController@createFolder');
Route::delete('admin/upload/folder', 'UploadController@deleteFolder');
});
到
Route::group([
'namespace' => 'Admin',
'middleware' => 'auth',
], function () {
Route::get('admin/upload', 'UploadController@index');
Route::post('admin/upload/file', 'UploadController@uploadFile');
Route::delete('admin/upload/file', 'UploadController@deleteFile');
Route::post('admin/upload/folder', 'UploadController@createFolder');
Route::delete('admin/upload/folder', 'UploadController@deleteFolder');
Route::resource('admin/post', 'PostController', ['except' => 'show']);
Route::resource('admin/tag', 'TagController', ['except' => 'show']);
});