简体   繁体   中英

Named routes not working in Laravel 5.2?

I have some named routes using the as parameter. Seems to work fine in 5.1, but when I use the same package in 5.2 it throws an error:

ErrorException in UrlGenerator.php line 307:
Route [/blog] not defined. (View: ...)

I've cleared all the caches to no avail.

php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan config:clear

Have dumped:

composer dump-autoload

But still doesn't work. When I do a php artisan route:list it seems ok to me:

+--------+----------+------------------+------------+----------------------------------------------------------------+------------+
| Domain | Method   | URI              | Name       | Action                                                         | Middleware |
+--------+----------+------------------+------------+----------------------------------------------------------------+------------+
|        | GET|HEAD | blog             | blog       | ...                                                            |            |
|        | GET|HEAD | blog/feed        | feed       | ...                                                            |            |

EDIT: To clarify one thing. I'm definitely using route('blog') when trying to generate the route. It's also working in Laravel 5.1 just fine. Not to mention it's with ALL the named routes not just blog in the particular example I presented.

Your error message is saying that you're trying to access a route with the name "/blog", however, your route is named "blog" (no slash). Somewhere you have the code route('/blog') , and this is causing your error; it should be route('blog') .

I am having the same issue with Laravel 5.2. I have 3 simple routes defined.

Route::get('/', function () {
    if (!empty(Auth::user())) {
        return view('auth.zipline');
    } else {
        return view('no_auth.login');
    }
});

Route::get('/login', function () {
    return view('no_auth.login');
});

Route::get('/pricing', function () {
    return view('no_auth.pricing');
});

The no_auth.login view is present (Text that says 'Login'). The no_auth.pricing view is present (text that says 'Pricing')

The / route works. I get the login view.

Neither of the other routes work. Going straight to /login or /pricing give me 404 errors. mod_rewrite is enabled on the server and I'm using the default Laravel 5.2 .htaccess file.

php artisan route:list looks like (notice the missing leading slashes?):

+--------+----------+---------+------+---------+------------+
| Domain | Method   | URI     | Name | Action  | Middleware |
+--------+----------+---------+------+---------+------------+
|        | GET|HEAD | /       |      | Closure |            |
|        | GET|HEAD | login   |      | Closure |            |
|        | GET|HEAD | pricing |      | Closure |            |
+--------+----------+---------+------+---------+------------+

The Routes file definitely has the leading slashes for /login and /pricing. I have run php artisan route:clear

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