我试图熟悉Laravel框架。我一直在关注一个教程,并坚持使用路由的一部分。我有一个简单的博客项目试图创建和编辑博客文章。我已经有2个其他对象的CRUD操作正常工作 - 其中一个是在路由中设置一个资源,就像我正在试图用这个一样。当试图做一个邮寄到以下几点:Laravel 5.2使用资源进行CRUD操作时,路由不工作
<form class="form-horizontal" role="form" method="POST"
action="{{ route('admin.post.store') }}">
我返回了一个错误“路线[admin.post.store]没有定义(查看:/家庭/流浪者/编码/测试开发/资源/意见/管理/后/ create.blade.php)”
现在我理解,这是它能够找到默认情况下,在基类中我的模型后对象的存储存储过程中的问题。我使用工匠和移植来创建这些对象并自动将它们绑定到数据库对象。 post.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
}
这里是 “路线” 文件夹内的文件web.php:
Route::get('/', function() {
return redirect('/blog');
});
Route::get('blog', '[email protected]');
Route::get('blog/{slug}', '[email protected]');
// 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', '[email protected]');
Route::post('admin/upload/file', '[email protected]');
Route::delete('admin/upload/file', '[email protected]');
Route::post('admin/upload/folder', '[email protected]');
Route::delete('admin/upload/folder', '[email protected]');
});
Auth::routes();
Route::get('/home', '[email protected]');
下面是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等级:
<?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,
];
}
}
检查与工匠的路由列表返回如下:
POST | admin/post | post.store | App\Http\Controllers\Admin\[email protected] | web,auth
GET|HEAD | admin/post | post.index | App\Http\Controllers\Admin\[email protected] | web,auth |
GET|HEAD | admin/post/create | post.create | App\Http\Controllers\Admin\[email protected] | web,auth |
PUT|PATCH | admin/post/{post} | post.update | App\Http\Controllers\Admin\[email protected] | web,auth |
DELETE | admin/post/{post} | post.destroy | App\Http\Controllers\Admin\[email protected]
我不理解这就是为什么其他物体看起来完全相同的条件下进行工作。表单帖子的做法有一点不同,我认为是造成这个问题。我觉得还有其他的东西需要配置或“连接”以确保{{route()}}调用找到正确的url。
任何帮助是非常明显的。
您可以检查您使用'PHP工匠将所有路线:list' 。 –
我实际上做了这个检查,并且看到我的终端正确(至少我理解正确)显示。我更新了问题以显示与此对象调用相关的结果。 – Chris