dongyou7292 2014-03-10 23:34
浏览 117
已采纳

在laravel中传递参数

i have an index pages with a link_to:

<h1> User:</h1>
        @foreach ($user as $user)

            {{link_to("/users/{$user->username}",$user->username)}}
        @endforeach

then i have a route:

Route::get('/users/{{$username}}', function($username){

        return View::make('show')->with('username',$username);

});

Now, if i understand clear, i am passing username as parameter to function, and username is my url, now if i pass parameter to my show view,

<body>
    <div>       

        <h1> User:</h1>
            {{$username}}
    </div>
</body>
</html>

I should be able to see it in my page. Where i am wrong? I can't take a parameter from the url when i use get? Why i need to do:

Route::get('/users/{{$username}}', function($username){
$user=User::whereUsername($username)->fist();

        return View::make('show')->with('username',$user);
});
  • 写回答

1条回答 默认 最新

  • dqzpt40064 2014-03-11 01:23
    关注

    Your route is wrong, this is the correct one:

    Route::get('/users/{username}', function($username){
    
        $user=User::whereUsername($username)->fist();
    
        return View::make('show')->with('user',$user);
    
    });
    

    It's just

    /users/{username}
    

    and not

    /users/{{$username}}
    

    Also, you view will receive an user object, so you have to:

     <div>       
         <h1> User:</h1>
             {{$user->username}}
     </div>
    

    EDIT

    In Laravel there are 2 kind {}:

    1) In views you have to use {{}} or {{{}}} (escaped version). Inside them you put PHP code:

    {{$variable}}
    
    {{ isset($variable) ? $variable : 'default value' }}
    

    2) In routes, you just use {} and inside it it's not PHP, just a route parameter name, without $:

    /user/{name}
    
    /user/{id?} (in this case id is optional, might or might not be send)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题