April, 23 2020

Laravel Lessons: Routing

Today, we will be learning about Laravel routing as we build our mail sending app.

Routing is about adding paths allows people to access your web application. Yeah, its that simple. We call those paths routes.

Laravel Routes

Laravel has a dedicated folder called routes, which houses all the different kinds of routes that exists in Laravel. If you check the routes folder of our mailing app, you will see 4 routes files. First, let me explain those files.

api.php : This is where you can defined API routes. Every route defined here is automatically prefixed with "\api". Routes defined here are stateless.

Stateless means there is no record of previous request and each request has to be handled based entirely on information that comes with it.

channels.php : This is where you can define broadcasting channels for different events in our application. I usually use this when ever I want to implement realtime stuff, not all the time.

console.php : This is where you can define your own artisan commands. Lately I started using this a lot, usually to do some initial setups. All commands defined here are all closure based commands.

Basically a closure in PHP is a function that can be created without a specified name - an anonymous function.

web.php : This is where you can define the paths that are stateful. Routes defined here are session based. We will be focusing on this file all through this series.

Laravel Route Facade

Laravel route facade is what does all the magic inside the api.php and web.php files. I am going to get a bit more technical so as to me give you an understanding of what a Laravel facade is.

The word FACADE means the one exterior side of a building, usually the front. As we all know the front of a building is always beautiful. This is the same for Laravel Facade. All the code that makes routing to work resides inside illuminate/Routing/ directory but we access them through the Illuminate\Support\Facades\Route facade. This is possible because of Laravel service container(service container is another topic for another day).

Lets take a look at some of the route methods. Almost all the route methods accepts two parameters - the path and a callback(except a few of them).

Route::get('/', function () {
    return view('welcome');
});

This is the default route defined inside the web.php. This route tells laravel to display the resources/views/welcome.blade.php file when some one hits our base URL. The second parameter is a callback, in this case a function. We can also pass a controller name and function as a callback that will handle the request.

Route::get('/', 'HomeController@index');

This request will be processed inside the index method of the HomeController(we don't have any controller yet). Thats what I call a controller callback :)

...
class HomeController extends Controller
{
    function index(){
       return view('welcome');
    }
}

We all also have other other route methods like post(), put(), patch(), delete() that handle HTTP requests. These methods work just like the get() method.

Route Parameters

Passing variable through the URL is a common thing among developers and the easiest way to do this is by attaching a query string to the URL like this: http://litemailer.test?user\_id=1

In Laravel you can attach a variable to the URL without the traditional query string.

Defining a route with parameter is very simple. The parameter that is part of a route is enclosed inside a pair of curly brackets. Just like this:

Route::get('/{user_id}', function ($user_id) {
    return view('welcome');
});

We also need to make our function to accept a corresponding parameter $user_id like you saw from the code snippet above. This is all we need to do, Laravel will do the rest for us(magic!).

This is the same thing when we have a "controller callback" with a little difference.

Route::get('/{user}', 'HomeController@index');

Inside our HomeController class, our index method will accept the corresponding parameter. Like this:

 function index($user_id){
     return view('welcome');
 }

LiteMailer Routes

Now we know the basic routing mechanism in Laravel, lets setup the routes we are going to use for the next lesson.

In the next lesson, we will setup the frontend of our mailing app. We will need a Landing page, add email page and a send email page. We are going to need three routes and we already have one. Lets get to work...

Our web.php file should look like this:

...
Route::view('/', 'welcome');
Route::view('/email/add', 'add-email');
Route::view('/email/send', 'send-email');

Let me explain. The view() method is an equivalent of the get() method but the second parameter is a string variable not a callback. This string variable is the name of the view we want to display. This method doesn't need a callback function or a "controller callback".

Our next lesson will be about VIEWS & LAYOUTS.

Questions

You can drop your question on the comment section, send me a DM on twitter or send an email.

.

Join my inner circle newsletter

Be the first to hear about anything I publish, launch, or think is helpful for you. Subscribe here

Hey, have you tried Litehost lately ?

Litehost is a web hosting platform for PHP & Laravel developers with Composer, Git, PHP & CLI pre-installed. Try it now