Understanding Laravel Route Parameters

Route Parameters is used to get parameter behind URI

Example:

I have below URI:

http:://a2fahmi.com/course_id/23

Wanted:

I want to get the course id from the URI

Question:

How to get it … ?

Answer:

Open the routes/web.php and add this code:

Route::get(‘course_id/{id_number}’,function($id) {

echo ‘ID Course is ‘.$id;

});

Explain:

  • id_number is a parameter inserted in URI
  • function($id) this code mean that function accept the parameter and give to $id variabel.

Test in Localhost

parameter_routes_laravel

 

Route with more Parameter

We can give more parameter as required

Example:

I have a URI localhost like this:

http://localhost:8000/news/77/comment/22

Wanted:

I want to get news id and comment id

Question:

How do i get it … ?

Answer:

Open the routes/web.php and add this code:

Route::get(‘news/{article_id}/comment/{comment_id}’,function($id, $comment) {

echo ‘ID Article is ‘.$id;
echo ‘ The Comment id is ‘.$comment;

});

Result:

parameter_routes2_laravel

 

Note:

Route parameters cannot contain the – character. Use an underscore (_) instead.

 

Thanks For Reading

You may also like...

Leave a Reply

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