Routing

Routes in laravel start off easy, get complicated very fast and then go back to being easy once you understand the fundimentals of how it works. Below is an example of a route.

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

The “Route” class, allows you to use the “get” method. The get method requires 2 main arguments to function. First is the “URL”, essentially just the path in the URL bar on the browser, second, is a function, this function will always run when you go to that URL.

In the above example, we are using the “Route” class, with the “get” method, passing in the URL ‘/’, which is usually the home page, passing in an anonymous function that returns a “view“, views are a laravel construct. What view does is just return a template. To dig further and understand this more, lets change a few things and play around.

Route::get('/', function () {
    return 'All your base are belong to us!';
});

In this example, rather than returning a template, we are just retuning a string. Nice, now lets say we want to add another route, at a different URL.

Route::get('/enter', function () {
    return 'Where are we?!';
});

You can now see in the example that the url changed from ‘/’ to ‘/enter’. This now means if you type the url www.domain.com/enter then it will show the text ‘Where are we?!’.

Now something to note about the first argument of the get method isn’t always just a static url. It may be that you require a url that is more dynamic, that you would like to use to do some kind of personalized response, or get some info etc. Lets look at an example, of how this works.

Route::get('/greeting/{name}', function ($name) {
    return "Good Day $name";
    //Note the above was changed to double quotes, not single quotes
    //This is for string interpolation
});

In this example, you can see we have the url ‘/greeting’ but then it’s followed by ‘/{name}’. These curly braces are essentially saying that this value passed after ‘/greeting’ is dynamic, so you can use it as a variable. Now in order to use this variable from the URL, you need to also pass it to the function. Luckily laravel does all the hard work, so just add an argument to the function, with the SAME name as your URL paramater, and you can then use the variable in your response.

Now entering the URL www.domain.com/greeting/badpandacode will show the result

Good Day badpandacode

Neet, now the next step in this journey is working with redirecting routes. If in the example you are working with a team, and some route has changed in the time you have been working on it, you may want to redirect to it.

Route::get('/comein', function () {
    return 'Where are we?!';
});

Route::get('/enter', function () {
    return redirect('/comein');
});

In the above example, ‘/enter’ now goes to ‘/comein’. Well that was easy. However, now imagine you have 10 or 15 different routes and the urls are chaning and you are constantly having to update your code. This can get out of control very fast. Luckily there is an easy way to fix this, using named routes.

Route::get('/comein', function () {
    return 'Where are we?!';
})->name('gateway');

Route::get('/enter', function () {
    return redirect('gateway');
});

Now all we have done is added the ‘->name(gateway)’ to the object returned by the get method and changed the redirect from the url to the name ‘gateway’ in the ‘/enter’ route. All this does is give the ‘/comein’ route a name called gateway, so now we can referance gateway, instead of the actual URL, so now even if you change the ‘/comein’ URL to ‘/doorway’, because you are referancing the name gateway, it will still go to ‘/doorway’ anyway. While not life chaning right now, it will become very usefull later!

Lastly to round of the basics of routing. Lets talk about fallbacks, essentially where you end up if your URL is wrong. This works the same as the routing we have done so far, the only difference is, we aren’t going to use the ‘get’ method. We will be using a special method called ‘fallback’. Who would of thought.

Route::fallback(function () {
    return "It's dark in here";
});

You will notice that the fallback method takes only 1 argument, instead of 2. It’s because we are omitting the URL, if you have gone to the wrong URL, it just makes sense that this isn’t tied to any URL and just responds any time there isn’t a valid URL.

Now the full code thuse far.

//Basic route with a name
Route::get('/comein', function () {
    return 'Where are we?!';
})->name('gateway');

//Redirected route to a named route
Route::get('/enter', function () {
    return redirect('gateway');
});

//Fallback route incase none of the urls match
Route::fallback(function () {
    return "It's dark in here";
});

And now you have a basic understanding of routes in laravel!

people partying with confetti

Congratulations on taking another step on this journey

Leave a Reply

Your email address will not be published. Required fields are marked *