简体   繁体   中英

Laravel 5 named routes error

I defined this named route in my routes.php,

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

then I have this in my PagesController.php

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

My question is why http://localhost:8000/home produces this error?

NotFoundHttpException in RouteCollection.php line 161:

You haven't created a route for the url /home . You only created the / route.

To further explain your problem, you just named your route with home . That means you can link to {{url()->route('home')}} in your view to go to the URL / which will be your home page. You can also access this route from any controller using url()->route('home') .

If you want to access the page /home , you need to create a route for this.

eg:

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

'as'=>'home' just indicates the name for the route. Not the URI .

The route name is home so you can access it from a controller, or view using URL::route('home') . But the actual address in the URL is localhost:8000/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM