0

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.

Laurence
  • 58,936
  • 21
  • 171
  • 212
lcmp
  • 117
  • 1
  • 6
  • 1) check the source page to see if there is any output. 2)turn on error reporting. – itachi Feb 08 '14 at 17:33
  • @itachi the source page returns blank, just the html and body tags, not even the h1 tag is written. I have no errors on the php error log, but thank you for help. – lcmp Feb 08 '14 at 17:41
  • that's weird. are you using blade template? – itachi Feb 08 '14 at 17:42
  • Yes, the file is a blade template. – lcmp Feb 08 '14 at 18:23
  • check the view file. it is giving an output before `@extends`. prbably a BOM issue. but the point is, **Something** is before the `@extends`.... [provided you are using blade layouts] – itachi Feb 08 '14 at 18:29
  • I'm using plain html with blade syntax, not yet using the fancy blade layout, this was supposed to be my first and easy tutorial and everything was going well until this small problem. The file just have this `

    User {{$user->username }} page

    `
    – lcmp Feb 08 '14 at 18:37

2 Answers2

1

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>
Maurice
  • 1,342
  • 11
  • 21
0

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.

Miroslav Trninic
  • 3,327
  • 4
  • 28
  • 51
  • I guess I'm doing that already with `View::make('users.show', ['user' => $user]);` But I might be missing something... – lcmp Feb 08 '14 at 18:16
  • Ok, but do you have $user object avaliable in your controller ? And do you have route defined ? – Miroslav Trninic Feb 08 '14 at 18:21
  • I do have and the link created with `link_to()` creates the link to the correct page using the user name passed with `$user->username` as I mentioned in the topic. I must be doing something stupid somewhere else that I'm not quite getting, I'll keep trying to figure it out, thanks for your help anyway. =) – lcmp Feb 08 '14 at 18:28