
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Define a Route Differently for Non-Integer Parameters
The route parameters are available inside the curly braces and the name given has alphanumeric characters. Along with alphanumeric, you can also make use of underscore when choosing the name for your route params.
Syntax
The syntax for route parameters is as shown below ?
Route::get('/user/{myid}', function ($myid) { // });
Here myid the route parameter that we want to use further.
Multiple Route Params
You can have more than one route parameter as shown in the syntax below ?
Route::get('/students/{post}/feedbacks/{feedback}', function ($postId, $feedbackId) { // });
In above case there are two route params: {post} and {feedback}
Optional Params
You can also have optional params for your route. Optional params will not be available all the time and the same is denoted with ? after the parameter. The syntax for the optional param is as shown below ?
Route::get('/students/{myname?}', function ($myname = null) { return $myname; });
Route params validation
To make sure the route params are not an integer, you can do so using the where() method in your route defined.
Using where()
The where() method takes input as the param name and the rule that defines how you route the params name has to be.
Example 1
Route params checked using a regular expression that will accept any value between A-z or a-z.
Route::get('/student/{studentname}', function ($studentname) { // })->where('studentname', '[A-Za-z]+');
Output
The output for the above case is as follows. If you hit the URL with the student name as shown in the URL: http://localhost:8000/student/niya , it returns the name "niya" as shown in the following output
Now let us add some numbers to the name and see the output ?
We get 404 errors when we add niya123 to the URL. This is because we have defined a regular expression that will take only characters either uppercase or lowercase.
For example route param name with a mix of uppercase and lowercase ?
Let us test another example using where that has multiple params in it.
Example
Consider the following route that has a name and id in it ?
Route::get('/student/{studentid}/{studentname}', function ($studentid, $studentname) { return $studentid."===".$studentname; })->where(['studentid' => '[0-9]+', 'studentname' => '[a-z]+']);
In the above route, we have studentid and studentname as the route params. The rule on studentid is to take all integer values and studentname in all small case letters.
Let us test the same in the browser and see the valid URLs and invalid URLs. The output for the URL: http://localhost:8000/student/123/niya will be -
123 === niya
Output
The output for the URL: "http://localhost:8000/student/123/NIYA"
Since the studentname parameter has a rule that only small case letters are allowed, it will give 404 if there is an uppercase letter in the name.
Using whereAlpha()
Besides the where() method you can also make use of whereAlpha() .The whereAlpha() method takes in only characters(uppercase/lowercase).
Let us test one example of it.
Route::get('/student/{studentname}', function ($studentname) { return $studentname; })->whereAlpha('studentname');
The valid URL for the above case is: http://localhost:8000/student/Niya
The invalid URL for the above case is: http://localhost:8000/student/Niya123
Using whereAlphaNumeric()
Laravel does provide another method called whereAlphaNumeric() where you can use both characters as well as numbers.
An example of it is
Route::get('/student/{studentname}', function ($studentname) { return $studentname; })->whereAlphaNumeric('studentname');
The valid URL for the above is: http://localhost:8000/student/Niya123
The invalid URL for the above case is: http://localhost:8000/student/Niya@#123