doukan5332 2014-02-08 09:29
浏览 34
已采纳

Laravel 4.1 link_to

I'm just starting with Laravel and I was doing some tutorials and crashed into something that I don't seem to work out. The thing is, I'm creating a link_to in a file like this:

<li>{{ link_to("/users/{$user->username}", $user->username) }}</li>

This link works and the page /users/foo is created, but then, in that page I'm using the username to print a message like "User foo page" using the following code:

<body>
    <h1> User {{$user->username }} page</h1>
</body>

As I said, the page is created with the link http://myhost/users/foo but it just comes out blank, nothing is displayed and I can't seem to understand why. Thank you for your help.

EDIT: Sorry people, I was just being dumb and blind. I've forgotten to return the view. Jesus. With that corrected all went fine, thanks to all the people that helped me as they're solutions worked all fine with the view being returned.

  • 写回答

2条回答 默认 最新

  • dongyanggan3025 2014-02-08 13:37
    关注

    I don`t think you should use link_to:

      link_to('foo/bar', $title, $attributes, $secure);
    

    Try something like this:

    <li><a href="{{ URL::route('get_user', 'SomeUsername') }}">SomeUsername</a></li>
    

    routes.php:

    Route::get('users/{username}', array('as'=>'get_user', 'uses'=>'UserController@getUser'));
    

    UserController.php:

    class UserController extends BaseController{
    
    public function getUser($username){
       $user = User::where('username','=', $username)->first();
       return View::make('users.show')
                  ->with('user', $user);
    }
    

    blade view (users.show):

    <body>
      <h1> User {{$user->username }} page</h1>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部