欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

laravel 请求获得url的参数 $request segment 有大用 有大大用

How to access URL segment(s) in blade in Laravel 5?

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

2 Answers 正确答案

Try this

{{ Request::segment(1) }}

    The double curly brackets are processed via Blade -- not just plain PHP. This syntax basically echos the calculated value.

    {{ Request::segment(1) }} 

    Your Answer


    来自 https://stackoverflow.com/questions/31832819/how-to-access-url-segments-in-blade-in-laravel-5


    How to access url segment in controller in laravel 5.2

    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


     tomopongrac
    1 year ago(164,380 XP)

    @devanshul

    How is you route looking

    devanshul

    Route::get('admin/edit_country/{id}', 'AdminController@add_country');

    dd(Request::segments());

    dd(Request::segments());

    Did you import

    use Illuminate\Http\Request;
    
    pmall
     pmall
    1 year ago(546,345 XP)

    Why do you want to do this anyway? You can retrieve the id as a method argument.

    devanshul

    @tomo_pongrac yes i import Request. @pmall what do you mean by "method" can you please explain. How can i get segment in controller.

    tomopongrac
     tomopongrac
    1 year ago(164,380 XP)

    you can

    
    public function add_country($id)
        {
            echo $id;
        }
    

    https://laravel.com/docs/5.2/controllers#basic-controllers

    来自  https://laracasts.com/discuss/channels/laravel/how-to-access-url-segment-in-controller-in-laravel-52




    How to get current URL with parameters in Laravel 5



    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().

    How to get current URL without query string in Laravel
    1. return \URL::current();

    2. OR

    3. return \Request::url();

    How to get current URL with query string in Laravel
    1. return \Request::fullUrl();

    How to get url parameters in Laravel

    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

    1. $url_segment = \Request::segment(3);



    来自  http://www.expertphp.in/article/how-to-get-current-url-with-parameters-in-laravel-5

    普通分类: