Friday 27 March 2015

Routing in laravel with route parameters

This post is about routing in laravel with route parameters in the previous post we saw the basics of routing without route parameters.If you are new to routing in laravel then you should have a look at my previous post about basics of routing in laravel.So let's start
Why to use route parameters?
Many a times a we create a route which are similar,E.g if you have a blogging website your URL will look something like this learn-laravel.blogspot.in/articles/article-name.So if you have 50 articles in your website creating 50 routes in routes.php is not a good idea.If we observe the URL carefully
learn-laravel.blogspot.in/articles/ will remain same for all requested articles only the article name will differ from article to article.So by using route parameters we can group together similar looking paramters.Let's jump into pratical example.
Open routes.php file located in app/Http with your favorite text editor.
You will find some stuff already present in that file,those routes deals with routing the default hopepage of laravel framework.We have nothing to do with those routes since we are creating our new route.
Type the following line in routes.php file



1
2
3
Route::get('articles/{article_name}',function(){
	return 'Inside article_name was requested';
});

save the file open your browser and browse to localhost/laravel/public/articles/hello (According to my file structure).You will get something like this


Here hello becomes the article name.You can try anything e.g localhost/laravel/public/articles/blah-blah.This will still work but if you browse to localhost/laravel/public/articles
you will be presented with a whole page of error.So how to make this route common so that this works both for localhost/laravel/public/articles/hello and localhost/laravel/public/articles without error.
Go back to  routes.php file


1
2
3
4
5
6
oute::get('articles/{article_name?}',function($article_name=null){
	if ($article_name==null) {
		return 'No specific article was requested';
	}
	return $article_name .' article was requested';
});

Open your browser navigate to localhost/laravel/public/articles/hello you will get the article name in the output (hello in this case)


navigate to localhost/laravel/public/articles


So this route work in both the cases.
What we did is we made the {article_name} parameter optional by adding ? ({article_name}?) and passed the parameter in the anonymous function function($article_name=null).Since the parameter is optional it is necessary to assign null value.Then we did a basic check whether $article_name is null or !null and produced the result according.
You can use as many optional parameters in the route but make sure to assign null value.So this was all about routing in laravel with route parameters.



3 comments:

  1. Hello,
    Nice article has been posted. i did get lots of good points from here. i would like to visit here again in future too. keep posting such post.... Hire Laravel Developer

    ReplyDelete
  2. Hey Nice write up, keep Writing, Do you know you can also post your laravel related articles
    on http://www.laravelinterviewquestions.com/submit-an-article too.
    Thanks !

    ReplyDelete
  3. hello
    nice article.
    I learned a lot from this article.
    learn laravel in 2021
    https://creatulearning.com/services/laravel

    ReplyDelete