Get URL Segments In Controller And Blade Using Laravel
Có thể bạn quan tâm
Last updated on February 28, 2023
In this article, we will learn to get URL segments in controller and blade using the Laravel framework. We have the below route in the web.php that is located in the routes directory.
Route::get('/user/{id}', [UserController::class, 'edit']);This route creates the following URL,
http://127.0.0.1:8000/user/1In this URL 1 is the id of the user, which can be helpful for many tasks for example edit, delete, etc. This id is the 2nd or last segment of the URL.
We have a user controller and blade file along with the route. In the controller edit method simply returns a view of the blade file that is edit-user.blade.php.
Get URL Segments OR Parameter in Controller
Laravel allows getting URL segments in the controller. Laravel’s Request class has a segments() method that gets URL segments. It returns segments in array format and the first index starts from 0. Let’s pass the Request class instance into the edit method to access the segments() method.
public function edit(Request $request) { return view('edit-user'); }Now in edit() method, we can access segment() method
$segments = $request->segments();Request’s class method segments() has the ability to get all segments and convert them into an array. In the above-mentioned URL, there are two segments. So we have ‘user’ and 0 index and 1 id at 1 index. Following is the code and result,
$segments[0]; /* output: user */ $segments[1]; /* output: 1 */Get URL Segments OR Parameter in Blade File
In Laravel, we can directly use a few classes in the blade file, Request class is one of them. As we know that URL segments can be accessible through Request’s class method segments(). So open PHP tags and add the following code in the edit-user.blade.php file,
@php $segments = Request::segments(); @endphpNow, we have both segments that are stored in the $segments variable in the array. We can print the segments by using curly braces and index, following is the example with the result.
{{ $segments[0]; }} /* output: user */ {{ $segments[1]; }} /* output: 1 */Conclusion
To get URL segments in Laravel, you can use the segments() method from the Request class that returns all URL segments in array format.
Written byAyaz Ali Shah
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming. Share on: Related Posts- 1. Install Bootstrap 5 in Laravel 10
- 2. How to Get Client IP Address in Laravel 10?
- 3. How to Create Route in Laravel 10?
- 4. How to Create and Run Controller in Laravel 10?
- 5. Laravel 10 Eloquent Mutators and Accessors
Từ khóa » How To Use Segment In Laravel
-
How To Access URL Segment(s) In Blade In Laravel 5? - Stack Overflow
-
Laravel Get Url Segment Code Example
-
How To Access Url Segment In Controller In Laravel 5.2 - Laracasts
-
Get Current URL With Parameters In Laravel 6, 7 & 8 - SkillSugar
-
How To Get URL Segment In Laravel?
-
AltThree/Segment: A Segment Bridge For Laravel. - GitHub
-
Requests & Input - Laravel - The PHP Framework For Web Artisans
-
How To Get URL Segment In Laravel? - Onlinecode
-
How To Access Url Segments In Blade In Laravel 5
-
Analytics For PHP | Segment Documentation
-
Laravel Get Url Segment - MaxInterview
-
How To Get URL Segment In Laravel? - NiceSnippets
-
Laravel Segment Is An Opinionated, Approach To Integrating ...
-
Laravel Get URL Segment Example Tutorial