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.

有帮助吗?

解决方案

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>

其他提示

You have to pass that variable (object) to view, in order to echo it. It may look like this is your controller:

return View::make("index")->with("user",$user);

Now you can {{$user->username}} which is translated to echo.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top