Basic Routing with Laravel

Vitaliy Dotsenko
Legacybeta
Published in
4 min readApr 8, 2020

--

Laravel Framework like other progressive PHP frameworks has routing. But why is the routing important? We need a connection between our actions in the code and URI address and the HTTP methods. You are probably most familiar with GET and POST, but there are a handful of others that also work with Laravel.

All Laravel routes are defined in routes files which are located in the directory routes. By default Laravel creates four main files with definitions of routes:

· api.php

· channels.php

· console.php

· web.php

Those files contain routes for different layers of Laravel applications, e.g. api.php contains API routes and web.php contains only web routes of the application.

If you need to add a new file you should register it in app/Providers/RouteServiceProvider.php. This file’s responsible for loading all routes of the application.

You can create a new route using the anonymous function aka Closure or the string with the controller name and the action name separated by @ for example SomeController@index, in this case, the last part with index is an action name and the SomeController is controller name.

Also, you should define the HTTP method, Laravel supports some of them: GET, POST, PUT, PATCH, DELETE and OPTIONS.

To register a new web route, you have to add a new code to the file routes/web.php, the registration of the GET route with the Closure will be:

Route:get('/some-address', function () {return 'some text';});

The same route but using the controller name and the action name:

Route::get('/some-address', 'SomeController@index');

If you need to process the POST method you should change the static method get() of the Route class to post(), for example:

Route::post('/some-address', 'SomeController@index');

To process all of the methods you should use the static method any():

Route::any('/some-address', 'SomeController@index');

Or if you need to process two or more HTTP methods at the same time just use the static method match() :

Route::match(['get', 'post'], '/some-address', 'SomeController@index');

Laravel supports the routes with parameters that are retrieved from URI, if you need to capture a parameter you should define it using the syntax{some_param}, but Laravel has some limitations with parameter naming — you can’t use — in the name of the parameter instead of using it just use underscore_. This parameter will be injected into Closure or the action of the controller.

The example of the route with Closure and GET method with one parameter:

Route::get('/some-address/{some_param}', function ($someParam) {// Do something with $someParam});

The parameter name in the anonymous function (in this example is$someParam) can be any, only the order matters.

Above we’ve gotten acquainted with the required parameters, to define optional parameters you should just add ? after the name e.g. {some_param?}. If you try to open the above example without param like /some-address/ you’ll get 404 page not found error.

You can define multiply parameters in one line {some_param}/{next_param} , in the code it will looks like this:

Route::get('/some-address/{some_param}/{next_param}', function ($someParam, $nextParam) {// ...});

Sometimes it’s good to have the additional constraints in the route, for example, the parameter should accept only id number of some entity, in Laravel it implements by the where() method and regular expression:

Route::get('/some-entity-address/{id}', function ($id) {// ...})->where('id', '[0-9]+');

Another good thing in Laravel routes is “named routes”, they are usually needs if you need to generate the URL based on the route. It easy to be implemented by the name() method:

Route:get('/some-address', function () {return 'some text';})->name('someRoute');

after that you can call the function route() and get URL for the above route:

$url = route('someRoute');

or if you have the route with some parameter:

$url = route('someRoute', ['id' => 1]);

Laravel also has groups of routes. You can create them using the group() method. It’s useful if you have the list of routes when connected to the same stuff, e.g. the user profile and the routes to manipulate it, you can just put them to the one group. You can easily assign to the group middlewares, namespaces, route prefixes and also do subdomain routing.

For example, just using the prefix() method you can define URI prefix in the one place:

Route::prefix('user')->group(function () {Route::get('{id}', function () {// Return the user by id});Route::post('/', function () {// Create a new user});});

The first route is matched to URI with user id and GET e.g. /user/1 and the second — POST request to /user without any route parameters.

This is just an introduction to basic routing in Laravel. Laravel has an easy to use and flexible routing system which allows the user to create maintainable modern web applications.

--

--

Vitaliy Dotsenko
Legacybeta

I like coding, open-source software and hi-tech 🚀