拉拉维尔 5 命名路由错误


Laravel 5 named routes error

我在路由中定义了这个命名路由.php,

Route::get('/', [
    'as' => 'home',
    'uses' => 'PagesController@home'
]);

然后我在我的页面控制器中有这个.php

public function home() {
    return 'Welcome home!';
}

我的问题是为什么 http://localhost:8000/home 会产生此错误?

NotFoundHttpException in RouteCollection.php line 161:

您尚未为 url /home 创建route。您只创建了/路由。

为了进一步解释您的问题,您刚刚用 home 命名了您的路线。这意味着您可以链接到视图中的{{url()->route('home')}}以转到 URL /这将是您的主页。您还可以使用 url()->route('home') 从任何控制器访问此路由。

如果要访问页面/home,则需要为此创建路由。

例如:

Route::get('/home', ['as'=>'NameUrRoute', 'uses'=>'PagesController@SamplePage']);

'as'=>'home'仅指示路由的名称。不是URI.

路由

name home,因此您可以从控制器访问它,或使用URL::route('home')查看。但 URL 中的实际地址是localhost:8000/