Laravel Classroom image

Prashant  Nigam / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4|Last
Lessons:-Routes With Closures

4.2 Redirects and Named Routes
It would be kinda silly to look at named routes before seeing a method that uses them wouldn’t
it? Let’s have a look at the Redirect class, it can be used to Redirect to another route. It can be
used in a similar fashion to returning a view.
1 <?php
23 // application/routes.php
4 Route::get('/', function()
5 {
6 return Redirect::to('account/profile');
7 });
Lovely! Couldn’t be simpler, wait no, it could, and is! Lets take a look at a named route :
1 <?php
2
3 Route::get('account/profile', array('as' => 'profile', 'do' => function()
4 {
5 return View::make('account/profile');
6 }));
Instead of passing a Closure as the second parameter, we now pass an array with the key ‘do’
pointing to the closure. This allows us to add all kind of extra information to the route.
The ‘as’ key, assigns a nickname to our route, this is what named routing is all about. Let’s see
how it can be used to improve the Redirect:: from before.
1 <?php
2
3 Route::get('/', function()
4 {
5 return Redirect::to_route('profile');
6 });
There, now we have that nasty URI out of our code, very pretty. All of the classes or helpers
which refer to routes have a similar method to route to a named route. This can really clean up
your code, and makes it read like a book. Also, if you later decide to change the URI for a certain
page, you will not have to go back and change all of your links and redirects!

 
 
 

Prashant  Nigam

Skills    Laravel

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)