【发布时间】:2017-08-03 16:11:04
【问题描述】:
我正在学习 Laravel 5.4,但我无法解决这个问题。我像这样在视图刀片中添加了两条路线
<a href="{{ route('backend.blog.create') }}" class="btn btn-primary">Write post</a>
然后在我的路由 web.php 文件中我有
Route::resource('/backend/blog', 'Backend\BlogController');
在HomeController@index,我加载上面有按钮的索引页面,像这样
<?php
namespace App\Http\Controllers\Backend;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
class BlogController extends BackendController
{
protected $limit = 5;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::with('category', 'author')->latest()->paginate($this->limit);
$postCount = Post::count();
return view("backend.blog.index", compact('posts', 'postCount'));
}
...
}
Backend 目录中的 HomeController 持有
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Requests;
use Illuminate\Http\Request;
class HomeController extends BackendController
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('backend.home');
}
}
backend.home 上面的a href 会产生错误。为什么会这样?
完整的错误信息
ErrorException in UrlGenerator.php line 304:
Route [backend.blog.create] not defined. (View: /var/www/blog/resources/views/backend/home.blade.php)
【问题讨论】:
-
在终端中使用
php artisan route:list来检查路线名称。 -
route(..)用于命名路由。资源不注册任何命名路由。 -
@apokryfos,其实资源已经自动生成名字了
-
在你的路由中为什么你有控制器第二和功能第一?
Backend\BlogController这是错字吗?控制器作为第一个参数,然后是函数 -
我正在学习课程,但问题是课程使用的是
Laravel 5.2,而我使用的是5.4.. 有些事情有点不同