Try this
{{ Request::segment(1) }}
欢迎各位兄弟 发布技术文章
这里的技术是共享的
I have a url : http://localhost:8888/projects/oop/2
I want to access the first segment --> projects
I've tried
<?php echo $segment1 = Request::segment(1); ?>
I see nothing print out in my view when I refresh my page.
Any helps / suggestions will be much appreciated
The double curly brackets are processed via Blade -- not just plain PHP. This syntax basically echos the calculated value.
{{ Request::segment(1) }}
来自 https://stackoverflow.com/questions/31832819/how-to-access-url-segments-in-blade-in-laravel-5
PUBLISHED 1 YEAR AGO BY DEVANSHUL
I am working in Laravel 5.2 and i want to access URL segments in my controller. I am using
echo Request::segment(2);
but nothing is print. How can i get values from url in controller.
can you try this (return array of all segments) and see what you get
How is you route looking
Route::get('admin/edit_country/{id}', 'AdminController@add_country');
dd(Request::segments());
dd(Request::segments());
Did you import
use Illuminate\Http\Request;
Why do you want to do this anyway? You can retrieve the id as a method argument.
@tomo_pongrac yes i import Request. @pmall what do you mean by "method" can you please explain. How can i get segment in controller.
you can
public function add_country($id)
{
echo $id;
}
来自 https://laracasts.com/discuss/channels/laravel/how-to-access-url-segment-in-controller-in-laravel-52
In this post, I will tell you how can you get base url, current url with query string and url segment in Laravel.
While working with Laravel, i need many times to get current url and its segment, you can get current url by using helper function URL::current()
.
return \URL::current();
OR
return \Request::url();
return \Request::fullUrl();
You can easily get your url segment in Laravel by using segment
method. Let's assume i have a url with some query string.
www.domain.tld/user/list?page=2
$url_segment = \Request::segment(3);
来自 http://www.expertphp.in/article/how-to-get-current-url-with-parameters-in-laravel-5