Laravel Classroom image

Prashant  Nigam / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9|
Lessons:-Using Controllers

3.6 Advanced Routing
Now we are able to map our controllers and actions to URI’s in the format /controller/action/
param/param/... which is great, but we shouldn’t be restricted to using only this format.
Let’s see if we can break out of the mold. Earlier we placed a controller declaration in routes.php
but now let’s replace it with the following code.
1 <?php
2
3 //application/routes.php
4 Route::get('superwelcome/(:any)/(:any)', 'account@welcome');
Here we are saying, let’s send all web requests with the GET HTTP verb, and the address
/superwelcome/(:any)/(:any) to the welcome action of the account controller.
The (:any) segments are place-holders for our parameters, and will be passed in the order that
they are provided. Using (:num) will match only numbers, and using (:any?) will create an
optional segment.
So now a visit to /superwelcome/Dayle/Wales will show our lovely view page!
The advantage of defining routes is that we can have our URLs in whatever order we like, in
whatever format we like. For example we could also have..
1 <?php
2
3 //application/routes.php
4 Route::get('superwelcome/(:any)/(:any)', 'account@welcome');
5 Route::get('welcome/(:any)/to/(:any)', 'account@welcome');
Using Controllers 14
Now we have two different routes, with the same result page.
It is worth noting that Routes that are defined “higher up” in the routes.php file are given a
higher priority. With the following example..
1 <?php
2
3 // application/routes.php
4 Route::get('(:any)/(:any)', 'account@welcome');
5 Route::get('welcome/(:any)/to/(:any)', 'account@welcome');
..the second route would never be triggered because the (:any) in the first route would respond
to the welcome in the second route. This is a common mistake when starting out with Laravel.
Be sure to keep an eye on the priority of your routes!
We will be covering routing in more depth in the next chapter which will also cover routing with
closures instead of controllers.

 
 
 

Prashant  Nigam

Skills    Laravel

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

  Students (0)