Sunday, 16 August 2015

Routing:-
  • Most of the routes for our application in the app/Http/routes.php file, which is loaded by the App\Providers\RouteServiceProvider class.
  • The most basic Laravel routes simply accept a URL and a Closure :
                Route::get('/', function () {
                    return 'Hello World';
                    });

                Route::post('foo/bar', function () {
                    return 'Hello World';
                   });

              Route::put('foo/bar', function () {
                    return 'Hello World';
                      });

               Route::delete('foo/bar', function () {
                     return 'Hello World';
                });
  • We need to register a route that responds to multiple HTTP verbs, by using match method.
           Route::match(['get', 'post'], '/', function () {
                   return 'Hello World';
               });
  • It can also be done by using any method.
          Route::any('foo', function () {
                return 'Hello World';
           });
  • Sometimes, we need to capture a user's ID from the URL, by defining route's parameter:-
          Route::get('user/{id}', function ($id) {
               return 'User '.$id;
            });
  • For defining multiple route parameters:-
          Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
               return 'Hello World';
            });
  • The where method is used in routing concept.
  • The where method accepts the name of the parameter and defining a regular expression.
           Route::get('user/{name}', function ($name) {
                //
                })
             ‐>where('name', '[A‐Za‐z]+');

             Route::get('user/{id}', function ($id) {
               //
             })
             ‐>where('id', '[0‐9]+');
  • You may specify a name for a route(named routes) using the as array key when defining the route:-
          Route::get('user/profile', ['as' => 'profile', function () {
             //
          }]);
  • We can also specify route names for controller actions:-
          Route::get('user/profile', [
            'as' => 'profile', 'uses' => 'UserController@showProfile'
          ]);
  • If we are using route groups, we may specify an as keyword in the route group attribute array:-
          Route::group(['as' => 'admin::'], function () {
                             Route::get('dashboard', ['as' => 'dashboard', function () {
                                            // Route named "admin::dashboard"
                            }]);
            });
  • To assign middleware to all routes within a group, we may use the middleware key in the group attribute array.
           Route::group(['middleware' => 'auth'], function () {
                           Route::get('/', function () {
                                 // Uses Auth Middleware
                           });

           Route::get('user/profile', function () {
                    // Uses Auth Middleware
                   });
            });
  • Another common use-case for route groups is assigning the same PHP namespace to a group of controllers.
  • We can use the namespace parameter in our group attribute array to specify the namespace for all controllers within the group:-
          Route::group(['namespace' => 'Admin'], function()
                {
                    // Controllers Within The "App\Http\Controllers\Admin" Namespace
                    Route::group(['namespace' => 'User'], function()
                      {
                          // Controllers Within The "App\Http\Controllers\Admin\User" Namespace
                       });
                 });
  • By default, the RouteServiceProvider includes our routes.php file within a namespace group, allowing us to register controller routes without specifying the full App\Http\Controllers namespace prefix.
  • We only need to specify the portion of the namespace that comes after the base App\Http\Controllers namespace root.

No comments:

Post a Comment