This article will discuss on the usage of several feature owned in Laravel development. The feature is focus on using return view vs return redirect in laravel framework on developing web-based application. Somehow, based on several experiment or trial and error which it might be a true conclusion of maybe not below is how those two features on directing a process into a rendered page defined, below is the scenario on testing those two features :
There is a page for inserting a property containing data of information about server. This is made by using a CRUD (Create Update Delete)-type web-based application powering on Laravel framework. The minute on the process of inserting all the data or information required for a Server has successfully executed, it is obvious that the process need to be redirected to the list page of Server where it will be displayed in the top of the list the information about the last Server which has saved early.
This is where the return view and return redirect in my trial comes to a different. Below is the case on using return view which is basically a method for saving data or information regarding Server through the form prepared :
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(); return view("server.index"); }
But executing the above script, especially using the following line :
return view("server.index");
it is just for redirecting the process to the intended page which is the index.blade.php located in folder server which is mainly exist in the folder intended for all blade view template files in resources/view. The execution of the above will call the file index.blade.php for showing the list of the server. But it is actually generated the following error :
[2017-05-10 07:44:16] local.ERROR: ErrorException: Undefined variable: list_server in /var/www/html/laravel-project/storage/framework/views/7f3e2b293958d95a015993a2402e50518101efa8.php:50 Stack trace: #0 /var/www/html/laravel-project/storage/framework/views/7f3e2b293958d95a015993a2402e50518101efa8.php(50): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined varia...', '/var/www/html/i...', 50, Array) #1 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(42): include('/var/www/html/i...') #2 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('/var/www/html/i...', Array) #3 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/View/View.php(149): Illuminate\View\Engines\CompilerEngine->get('/var/www/html/i...', Array) #4 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/View/View.php(120): Illuminate\View\View->getContents() #5 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/View/View.php(85): Illuminate\View\View->renderContents() #6 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Http/Response.php(45): Illuminate\View\View->render() #7 /var/www/html/laravel-project/vendor/symfony/http-foundation/Response.php(201): Illuminate\Http\Response->setContent(Object(Illuminate\View\View)) #8 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1042): Symfony\Component\HttpFoundation\Response->__construct(Object(Illuminate\View\View)) #9 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(642): Illuminate\Routing\Router->prepareResponse(Object(Illuminate\Http\Request), Object(Illuminate\View\View)) #10 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #11 /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))
The execution of the page will generate an error claiming that the variable named ‘$list_server’ has not being initialized. But surely that problem will be solved just by attaching the line of code for returning the process to a view as follows :
$listServer = Server::all(); return view('server.index')->with('list_server', $listServer);
But instead doing the above stuff with the additional line of source code for feeding the list page server with the variable assigned with the execution of the prior line of code getting all the list of server’s record. Changing ‘return view’ into ‘return redirect’ in my case is another solution which can be looked for. Just change it into the following line of code from the old one using ‘return view’ as shown below :
return view("server.index");
into the following line of code redirecting the process according to the route defined as follows :
return redirect("/server");
It is actually redirecting to the folownng route defined in a route file named web.php as follows :
Route::get('/server','ServerController@index');
So, by executing the route defined as shown above the list page of Server will be displayed normally without having to initialize the list_server variable because it has already been handled in the ‘index method’ defined in ServerController. Below is the content of the index method inside ServerController file :
public function index(Request $request) { //public function index() $list_server = Server::orderBy('id_server','DESC')->paginate(5); return view('server.index', compact('list_server')) ->with('i', ($request->input('page', 1) - 1) * 5); }
Compare it with the above method redirection which is only to list all of the records of the Server. It is totally different with displaying all of the record of Server. So, instead of adding lines of code for retrieving records in a method which is intended to save record, just direct it to another method which is mainly intended to display the whole records.
One thought on “return view vs return redirect in Laravel”