Framework: [5.4] Route not defined (consistently configure the name of the router)

Created on 1 May 2017  路  18Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.21
  • PHP Version: 7.1

Description:

InvalidArgumentException in UrlGenerator.php line 304:
Route [home] not defined.

Steps To Reproduce:

If requried custom routes.php from ServicesProvider and
If you initialize the name of the router at the last moment, this router is not listed (router=>nameList)

Route::get('/')->uses('Controller@index')->name('home');

// InvalidArgumentException in UrlGenerator.php line 304:
// Route [home] not defined.
route('home') ;

If you change the initialization of the name places the router, then everything is okay.

Route::name('home')->get('/')->uses('Controller@index');

// this OK!
route('home') ;

Most helpful comment

So I encountered this in relation to testing a package (using orchestra/testbench). I found that if I used Auth::routes() in the test scenario, subsequent calls to route('login') would fail with Route [login] not defined.

I.e. the following fails in a test:

Auth::routes();

route('login'); // throws exception as login is not defined

Encountered when updating to Laravel 5.7 because that adds an authentication middleware that has a call to route('login') within it here:
https://github.com/laravel/laravel/blob/develop/app/Http/Middleware/Authenticate.php#L17

Having dug into this, the difference between the test scenario and a real application is that the real application would define its routes via its RouteServiceProvider. Within that service provider, the name and action lookups are refreshed here:
https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php#L35-L38

The solution in setting up the test scenario is therefore to do the following:

Auth::routes();
app('router')->getRoutes()->refreshNameLookups();
app('router')->getRoutes()->refreshActionLookups();

route('login'); // this now works

Not ideal but sorts out testing the package. The fundamental cause is if you use the name() method after the route has been created by the router, it has already been added to the router's route collection - so the name is set after the route collection has added the route to its internal map of route names.

Posting this because this took a while to find the solution for, so hopefully I can save some other people a lot of time!

/cc @themsaid

All 18 comments

Where do you call route('home')?

Also

Route [art.um.search] not defined is not related to the route you call. Please share the correct error message if any.

Looks to me something is wrong with your code.

Closing for lack of activity.

I have a similar problem when creating routes in tests. The order of name() and get() seems important. Here is a simple example :

<?php

namespace Tests;

use Illuminate\Support\Facades\Route;
use Tests\TestCase;

class RouteNameTest extends TestCase {
    public function testRouteName()
    {
        // The following works :
        // Route::name('test.test')
        //     ->get('/test', function() {
        //         return 'Hello world!';
        //    });

        // But this does not work :
        Route::get('/test', function() {
            return 'Hello world!';
        })->name('test.test');

        $uri = route('test.test');
        $response = $this->get($uri);
        $response->assertSee('Hello world!');
    }
}

Laravel Framework 5.4.28
PHP 7.1.0

Result of the above test:

PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

Route [test.test] not defined.
[project-root]\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php:304
[project-root]\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:711
[project-root]\tests\RouteNameTest.php:22

Time: 543 ms, Memory: 8.00MB

Unfortunately, there is such a piercing, but does not reach, describing it more de-atal (((
If in brief, then I created my service provider and in it I connect router files.
If I specify in the files of the router:

Route::get('..', '...')->name('test_1');
route('test_1');

That, the error InvalidArgumentException in UrlGenerator.php takes off
And if you do this:

Route::name('test_1')->get('..', '...');

That there is no mistake

I am seeing the same as @kozlikov.

//Route::get('/activity/categories', 'Admin\Activities\CategoryController@index')->name('activity-categories');

Route::name('activity-categories')->get('/activity/categories', 'Admin\Activites\CategoryController@index');

//Route::get('/activity/{id}/categories', 'Admin\Activities\CategoryController@byType')->name('activities-categories-type');

Route::name('activity-categories-type')->get('/activity/{id}/categories', 'Admin\Activities\CategoryController@byType');

The commented lines result in the route not defined error. If name is called first then it works as it should.

The error also only occurs when the route() helper function is called in the blade template. Not when navigating to the route itself.

Next ErrorException: Route [admin.activity-categories-type] not defined. (View: C:\inetpub\wwwroot\appraisal-merit\resources\views\admin\activities\types\all.blade.php) in C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php:304
Stack trace:
#0 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\View\Engines\PhpEngine.php(44): Illuminate\View\Engines\CompilerEngine->handleViewException(Object(InvalidArgumentException), 1)
#1 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\View\Engines\CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('C:\\inetpub\\wwwr...', Array)
#2 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\View\View.php(137): Illuminate\View\Engines\CompilerEngine->get('C:\\inetpub\\wwwr...', Array)
#3 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\View\View.php(120): Illuminate\View\View->getContents()
#4 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\View\View.php(85): Illuminate\View\View->renderContents()
#5 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Http\Response.php(38): Illuminate\View\View->render()
#6 C:\inetpub\wwwroot\appraisal-merit\vendor\symfony\http-foundation\Response.php(201): Illuminate\Http\Response->setContent(Object(Illuminate\View\View))
#7 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Router.php(617): Symfony\Component\HttpFoundation\Response->__construct(Object(Illuminate\View\View))
#8 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Router.php(574): Illuminate\Routing\Router->prepareResponse(Object(Illuminate\Http\Request), Object(Illuminate\View\View))
#9 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#10 C:\inetpub\wwwroot\appraisal-merit\app\Http\Middleware\CheckRole.php(26): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#11 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): App\Http\Middleware\CheckRole->handle(Object(Illuminate\Http\Request), Object(Closure), 'administrator')
#12 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#14 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#15 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#16 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php(43): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#17 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure))
#18 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(65): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#20 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#21 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#23 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#24 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#25 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#26 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#27 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#29 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#30 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#31 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#32 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#33 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#34 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#35 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Router.php(576): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#36 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Router.php(535): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#37 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Router.php(513): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#38 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#39 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#40 C:\inetpub\wwwroot\appraisal-merit\vendor\barryvdh\laravel-debugbar\src\Middleware\Debugbar.php(51): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#41 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Barryvdh\Debugbar\Middleware\Debugbar->handle(Object(Illuminate\Http\Request), Object(Closure))
#42 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#43 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#44 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#45 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#46 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#47 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#48 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#49 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#50 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#51 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#52 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#53 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#54 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#55 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#56 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#57 C:\inetpub\wwwroot\appraisal-merit\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#58 C:\inetpub\wwwroot\appraisal-merit\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#59 C:\inetpub\wwwroot\appraisal-merit\server.php(21): require_once('C:\\inetpub\\wwwr...')
#60 {main}  

Same here (Laravel 5.5).

Defined as:

Route::any('/alta', 'Garage@signup')->name('web.garage.signup');
Route::any('/location/city/{_id?}', 'Location@city')->name('web.location.city');

FIRST ROUTE /alta

Work on Ubuntu + PHP 7.1
Work on Mac OS X (MAMP PRO) + PHP 7.0

SECOND ROUTE /location/city/{_id?}

Work on Ubuntu + PHP 7.1
Fail on Mac OS X (MAMP PRO) + PHP 7.0

Same code, different environments.

Updated routes definition as:

Route::name('web.garage.signup')->any('/alta', 'Garage@signup');
Route::name('web.location.city')->any('/location/city/{_id?}', 'Location@city');

All routes works on all environments.


UPDATED: not working on all enviroments.

Solved deleting bootstrap/cache/routes.php file.

@eusonlito Unfortunately, cleaning the cache and manually deleting the cache of the routers does not help.

@themsaid Maybe all this should open the topic?

don't use a {{ route('/') }} command on views use {{ url('/') }}

En Web:
Route::group(['prefix'=>'admin','as'=>'admin'], function(){
Route::resource('role','Cruds\RoleController');
Route::resource('category','Cruds\CategoryController');
});

En php artisan route:list
| POST | admin/category | admincategory.store | App\Http\Controllers\Cruds\CategoryController@store | web,auth

En create.blade.php:
{!! Form::open(['route'=>'admin.category.store','class'=>'form']) !!}
{!! Field::text('name') !!}
{!! Field::text('description') !!}
{!! Form::submit('Guardar',['class'=>'btn btn-success']) !!}
{!! Form::close() !!}
En mi Navegador:
http://scrapfusion.pym:8090/admin/category/create

Sale:
Route [admin.category.store] not defined. (View: C:\laragon\www\Scrapfusion\resources\views\cruds\category\create.blade.php)

No he logrado corregirlo

Today, after using git merge with another branch i got conflicted then after i resolved it. I got same error as @pedrojosecruzguevara
Laravel 5.6

Update
I just resolved that. In my case. I put same method for 2 routes

$this->get('my-account', 'Controller@function')->name('get-my-account');
$this->get('my-account', 'Controller@function')->name('post-my-account');

it should be

$this->get('my-account', 'Controller@function')->name('get-my-account');
$this->post('my-account', 'Controller@function')->name('post-my-account');

the different are i placed 2 get methods with same route. Change one to POST and it works

@crazyfree if you are using same Controller@function to resolve GET and POST routes, you can use:

$this->any('my-account', 'Controller@function')->name('my-account');

So I encountered this in relation to testing a package (using orchestra/testbench). I found that if I used Auth::routes() in the test scenario, subsequent calls to route('login') would fail with Route [login] not defined.

I.e. the following fails in a test:

Auth::routes();

route('login'); // throws exception as login is not defined

Encountered when updating to Laravel 5.7 because that adds an authentication middleware that has a call to route('login') within it here:
https://github.com/laravel/laravel/blob/develop/app/Http/Middleware/Authenticate.php#L17

Having dug into this, the difference between the test scenario and a real application is that the real application would define its routes via its RouteServiceProvider. Within that service provider, the name and action lookups are refreshed here:
https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php#L35-L38

The solution in setting up the test scenario is therefore to do the following:

Auth::routes();
app('router')->getRoutes()->refreshNameLookups();
app('router')->getRoutes()->refreshActionLookups();

route('login'); // this now works

Not ideal but sorts out testing the package. The fundamental cause is if you use the name() method after the route has been created by the router, it has already been added to the router's route collection - so the name is set after the route collection has added the route to its internal map of route names.

Posting this because this took a while to find the solution for, so hopefully I can save some other people a lot of time!

/cc @themsaid

  • Laravel Version: 5.4.21
  • PHP Version: 7.1

Description:

InvalidArgumentException in UrlGenerator.php line 304:
Route [home] not defined.

Steps To Reproduce:

If requried custom routes.php from ServicesProvider and
If you initialize the name of the router at the last moment, this router is not listed (router=>nameList)

Route::get('/')->uses('Controller@index')->name('home');

// InvalidArgumentException in UrlGenerator.php line 304:
// Route [home] not defined.
route('home') ;

If you change the initialization of the name places the router, then everything is okay.

Route::name('home')->get('/')->uses('Controller@index');

// this OK!
route('home') ;

Worked for Me Like a Charm....Thanks Guys

<form method="POST" action="{{Request::root()}}/someroute}}">

this one helped me)

Was this page helpful?
0 / 5 - 0 ratings