我试图熟悉Laravel框架。我一直在关注一个教程,并且陷入了路由的一部分。我有一个简单的博客项目,试图创建和编辑博客文章。我已经有两个其他对象CRUD操作正常工作 - 其中一个是在路由中设置了一个资源就像我正在尝试使用这个。在尝试发布以下内容时:
Exception
我收到错误"路由[admin.post.store]未定义。 (查看:/home/vagrant/Code/test-dev/resources/views/admin/post/create.blade.php)"
现在我理解这是一个问题,因为它能够找到默认存储在我的模型post对象的基类中的存储过程。我使用artisan和migration来创建这些对象并自动将它们绑定到DB对象。 Post.php模型对象发布在下面:
<form class="form-horizontal" role="form" method="POST"
action="{{ route('admin.post.store') }}">
这是&#34; routes&#34;中的web.php文件。文件夹:
<?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
}
这是PostController
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');
最后 - PostCreateRequest类:
<?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.');
}
}
使用Artisan检查路线列表将返回以下内容:
<?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,
];
}
}
我不明白这就是为什么其他物体似乎在完全相同的条件下工作的原因。表单帖子的完成情况有一点不同,我认为这是导致问题的原因。我觉得还有其他东西需要配置或连接&#34;确保{{route()}}调用找到正确的URL。
非常感谢任何帮助。
答案 0 :(得分:1)
通过查看您的路线列表,确认您的表单中没有名为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']);
});
答案 1 :(得分:0)
尝试更改和更新您的作曲家:
$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']);
});