- This topic has 0 replies, 1 voice, and was last updated 1 year, 2 months ago by
Chinomnso.
- AuthorPosts
- December 13, 2019 at 10:35 am #82010Participant@chinomnso
One very important concept every Laravel developer must be familiar with is Routing. This article aims to teach basic routing in an easy-to-understand way. Therefore, I’ll be touching just what you need to get up-and-running. You can always pick things up along the way and build on what you are about to learn here. If you want to learn how to install Laravel on your local machine, be sure to read this topic.
Routing is a mechanism in MVC architecture that an application uses to decide which method is to be executed when a request is made to a controller class. In the absence of routing, a request cannot be mapped to a controller method. In Laravel, there are several route files including api.php, channels.php, console.php and web.php. All of these can be found in the routes directory. This article focuses on working with the web.php file because we’re dealing only with web routes.
Using the web.php route file, you can route or direct URLs to controllers in your application. So if a user types ‘yoursite.com/about’, it is in this file that you define where the user is taken to and what they see.
To create a route, open the web.php file, add the following code and save it:
123Route::get('/testing', function(){return view('welcome');});The route above tells your application that if a user types ‘yoursite.com/testing’ in the browser, they should be taken to the welcome view. By default, the welcome view is the homepage for a Laravel application. So, if you try loading that URL in your browser, it should redirect you to the homepage of your Laravel installation.
Sometimes, you may need to make use of other details in a URL. These can be passed through a route and captured in your code, then you can do whatever you choose with the parameter. Notice how this is done in the code below:
123Route::get('user/{name}', function ($name) {return 'The name of the user is '.$name;});The code above is similar to the previous code block, except for a few things. Rather than taking the user to a view, this route displays text defined in the return construct. The variable passed to the URL is also appended to the text. Copy this code, and try running a URL like this in your browser: yoursite.com/user/Joe. You should see a webpage displaying something like “The name of the user is Joe.”
Inside your route, you can also define several parameters. Note, however, that your route parameters must always be enclosed in curly braces
{ }
. The parameters may also contain alphanumeric characters and underscores ( _ ). Therefore hyphens ( – ) are not allowed. Note too, that route parameters are placed in the route callbacks or controllers based on their order without necessarily making them correspond exactly to their names. I’ll explain that after the code block below.123Route::get('user/{name}/age/{userage}', function ($nameOfUser, $ageOfUser) {//});In the code above,
($nameOfUser, $ageOfUser)
were defined in the same order as'user/{name}/age/{userage}'
were defined. But they were not named exactly as the parameters were named. If you reverse the arrangement of the callbacks (($ageOfUser, $nameOfUser)
), you will not get expected results.Parameters too can be optional. This means that you can choose to pass them in the URL or not. A parameter can be marked as optional by placing a question mark (?) next to it. You need to give the route’s corresponding value a default value. This way, if a user’s actions on your application do not provide a value, your application has something – a default value – to fall back to.
1234567Route::get('company/{name?}', function ($name = null) {return $name;});Route::get('company/{name?}', function ($name = 'Efyko') {return $name;});There is a lot more you can do with routes in Laravel. But this is not an advanced article on Laravel routes. Like I mentioned earlier, it is one aimed at helping you to understand just what you need to spin up a route and get your application working. If you want more information, read up the official Laravel documentation on routes. If you’re having difficulties understanding it, just come back here and ask your questions. I’ll be lurking around to answer them.
- AuthorPosts
- You must be logged in to reply to this topic.