Pass Parameters to Your Laravel Route
https://example.com/results.php?page=4
, we need to use the $_GET
global array, i.e. $_GET[‘page’]
.Passing a Single Argument
In Laravel, we can pass arguments to the route file just as simply.
<?php
use Illuminate\Support\Facades\Route;
Route::get('/about/{name}', function ($name) {
return $name;
});
To declare a parameter, we need to surround the url parameter with curly braces. In the example above, we are expecting a “name.” An example of this route might look like this: http://0.0.0.0/about/Dino
.
The route is defined as is. That means that in this instance, if we try to visit http://0.0.0.0/about
, the route file will not know how to process it.
Next, we need to pass our argument to the anonymous function. We just specify the argument in our function declaration: function($name)
. The function body now has access to the argument. For the sake of simplicity, we’ll just return it now. If you visit the route http://0.0.0.0/about/Dino
, you’ll see Dino
.
We don’t have to name our function parameter the same as the url parameter that we specified between the curly braces. We can use any valid variable name.
<?php
//...
Route::get('/about/{name}', function ($n) {
return $n;
});
Even though we named the function parameter $n
, it still works.
Passing Multiple Arguments
What if we need to pass more arguments? Not any more difficult than what we just did.
<?php
// ...
Route::get('/about/{name}/{age}', function ($name) {
return $name;
});
http://0.0.0.0/about/Dino/85
, we get Dino
. We have to declare the age
parameter as well.<?php
// ...
Route::get('/about/{name}/{age}', function ($name, $age) {
if ($age > 100) {
return "You're pretty old" . $name;
}
return $name . ", you're only " . $age . " years old.";
});
The response that I get is: Dino, you’re only 85 years old.
Can we swap our parameter declaration?
<?php
// ...
Route::get('/about/{name}/{age}', function ($age, $name) {
if ($age > 100) {
return "You're pretty old" . $name;
}
return $name . ", you're only " . $age . " years old";
});
We cannot. The reason why we can name our function parameters whatever we like is because it binds the arguments in order. {name}
comes first and then {age}
. That means that we need to bind {name}
to the first parameter and {age}
to the second parameter.
Can we add other url strings between arguments? Yes we can.
<?php
// ...
Route::get('/about/name/{name}/age/{age}', function ($age, $name) {
if ($age > 100) {
return "You're pretty old" . $name;
}
return $name . ", you're only " . $age . " years old";
});
The url that we need to visit in this instance is: http://0.0.0.0/about/name/Dino/age/102
.
Optional Parameters
Sometimes we need to call the page without passing any arguments. In the previous examples, we could only visit the about page when we add additional arguments. What if we wanted to make the argument optional?
<?php
// ...
Route::get('/users/{name?}', function ($name = null) {
if ( $name === null ) {
return "See all of our users below...if we had access to the DB";
}
return "This is our user: " . $name;
});
Our {name?}
now contains a question mark at the end. The question mark is what signifies that the argument is optional. In our function declaration, if we don’t specify a default value, the route will fail when the user visits a route without the additional argument. But, we have everything defined so it should all work.
If we visit http://0.0.0.0/users
, we get: See all of our users below…if we had access to the Database
.
If we visit http://0.0.0.0/users/Dino
, we get: This is our user: Dino
.
Forcing URL Constraints
Sometimes we need to make sure that the argument passed is an integer or a string or even that it’s a specific string, such as germany
. Laravel allows for this through Regular Expressions, which you can do whatever you’d like with, or through a few built in functions. To add the regular expression, you chain on where
to the route.
<?php
// ...
Route::get('/countries/{country}', function ($country) {
return "The country you selected is: " . $country;
})->where('country', '[A-Za-z]+');
The first parameter in the where
method is the name of the url parameter, i.e. {country}
. The second parameter is the regular expression.
We can check multiple parameters. We just need to pass an array with key/value pairs to the where
method instead of two strings like we did earlier.
<?php
// ...
Route::get('/countries/{country}/{state}', function ($country, $state) {
return "The country you selected is " . $country . " and you live in the state: " . $state;
})->where([
'country' => '[A-Za-z]+',
'state' => '[A-Za-z]{2}+'
]);
The code above specifies that the country
should only contain alpha characters. The state
specifies that we should have 2 alpha characters. For example, http://0.0.0.0/countries/usa/fl
is a valid url, whereas http://0.0.0.0/countries/usa/florida
is not a valid url.
You could have chained on multiple where
methods if you didn’t want to pass an array.
<?php
// ...
Route::get('/countries/{country}/{state}', function ($country, $state) {
return "The country you selected is " . $country . " and you live in the state: " . $state;
})->where('country', '[A-Za-z]+')
->where('state', '[A-Za-z]{2}+');
There are commonly used regular expressions that you can use, such as:
whereNumber
whereAlpha
whereAlphaNumeric
whereUuid
whereIn
<?php
// ...
Route::get('/cars/{car}', function ($car) {
return "Your car is: " . $car;
})->whereAlpha('car');
If you visit http://0.0.0.0/cars/Porsche
, you’ll see: Your car is: Porsche
.
You can chain multiple expressions again.
<?php
// ...
Route::get('/cars/{car}/{cylinders}', function ($car, $cylinders) {
return "The car you selected is a " . $car . " with " . $cylinders . " cylinders." ;
})->whereAlpha('car')
->whereNumber('cylinders');
http://0.0.0.0/cars/Porsche/6
outputs The car you selected is a Porsche with 6 cylinders.
We can pass an array as an argument if we have multiple checks.
<?php
// ...
Route::get('/cars/{make}/{model}/{cylinders}', function ($make, $model, $cylinders) {
return "The car you selected is a " . $make . " " . $model . " with " . $cylinders . " cylinders." ;
})->whereAlphaNumeric(['car', 'model'])
->whereNumber('cylinders');
http://0.0.0.0/cars/Porsche/911/6
outputs The car you selected is a Porsche 911 with 6 cylinders.
And that’s it for route parameters. There are a few more things that we didn’t cover since we haven’t gotten to that yet, like dependency injection.
Laravel Series
Continue your Laravel Learning.
How To Route in Laravel
Laravel – P4: Basic Routing and View Introduction
How do you get to a specific view in Laravel without going through a Controller? It can be done through the route itself and we’ll explore that in this article.
Pass Parameters to Your Laravel Route
Laravel – P5: Route Parameters
Frequently we need to pass arguments to our route file via URL parameters. You might know how to do it with PHP but it’s even easier with Laravel.
How to Pass Arguments to Views
Laravel – P6: Passing Arguments to Views
We know how to grab URL parameters and return them from our route, but how do we pass arguments to the view?