Laravel Error Message : Route not defined

Posted on

This article is an article where the main point of the writing is the discussion on how to solve the problem caused by an error message specified in the title of this article which is “Route not defined”. But to be more precise, the route which is considered as not defined is mentioned as part of the error message. The error message which is part of the route that is not defined can be vary depends on the route which is has already set in the route file used by Laravel framework web-based application. In Laravel 5.3, the location of the route file is in routes folder. The name of the route file is web.php. But on the older version of Laravel, it is located in App\Http\routes.php.

The following is the general information about the scenario which is going to be presented so that it is clear where is the error is actually happened. A CRUD (Create-Update-Delete) simple web-based application is written to save a property or information of a server. So, it is used to maintain the information about list of servers. But upon the process of saving a server information form when it is going to be redirected into the page of server list, an error is triggered.

Below is the example of the error which is related with the route problem :

InvalidArgumentException in /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php line 314 Route [/server] not defined.

The above error message is just part of the error message retrieved from firebug plugins installed in Mozilla Firefox web browser. As it can be seen from the above error message, it can be retrieved clearly that the route which is becoming the trigger of the error is the ‘/server’ route.

[2017-05-10 12:54:54] local.ERROR: InvalidArgumentException: Route [/server] not defined. in /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:314
Stack trace:
#0 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Redirector.php(157): Illuminate\Routing\UrlGenerator->route('/server', Array)
#1 /var/www/html/laravel-project/app/Http/Controllers/ServerController.php(247): Illuminate\Routing\Redirector->route('/server')
#2 [internal function]: App\Http\Controllers\ServerController->checkGeneral(Object(Illuminate\Http\Request))
#3 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array)
#4 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller->callAction('checkGeneral', Array)
#5 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(189): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\ServerController), 'checkGeneral')
#6 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(144): Illuminate\Routing\Route->runController()
#7 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(642): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#8 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#9 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#10 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))

The following is actually the error which is triggered :

return redirect()->route('/server');

It exists in a method which is used to direct the location of the page to the one defined as a route in a route file named route.php. Below is the route definition :

Route::get('/server','ServerController@index');

To solve the above problem, just change the way how the redirect process for redirecting the page after executing the method. Below is the line which is needed to be changed :

return redirect()->route('/server');

Change it into the following line :

return redirect('/server');

The description of the application’s flow is shown below :

An execution of the program through a script is initiated from a blade view template access for creating a form to create a specific object named Server. The snippet code for the form is shown below :

{{ Form::open(array('url' => '/server/save_data', 'class' => 'form-horizontal', 'id' => 'home_form'))}}
...
{{Form::close}}

As it can be seen from the above form definion, it will definitely call an url specified as /server/save_data. The URL itself is going to be checked through the router file definition and it is matched with the following route definition :

Route::post('/server/check_data','ServerController@saveData');

It is matched with the method defined in the controller named ‘ServerController’ called saveData as specified in the route definition above. The content of the method itself is shown as follows :

public function saveData(Request $request) {
        $server = new Server();
        $server->type = $request->input("type");
        $server->serial_number = $request["serial_number"];
        $server->label = $request["label"];
        $server->ip_internal = $request["ip_internal"];
        $server->ip_public = $request["ip_public"];
        $server->ip_management = $request["ip_management"];
        $server->label = $request["label"];
        $server->year = $request["year"];
        $server->warranty = $request["warranty"];
        $server->save();
        ...  

After creating a new instance of an object named Server and retrieving all the information from the data or information inserted in the form which has already been defined above, the process is ended by saving it into the database. But after finishing all the process, it must be redirected to a certain location which in this context, it is referred to the list of server which has been successfully inserted or saved. It is done by passing it to the index method of ServerController which is defined in the route file as follows :

Route::get('/server','ServerController@index');

So, in the above part it has been described by changing the redirect way in just one line of code successfully solve the problem.

One thought on “Laravel Error Message : Route not defined

Leave a Reply