Question

I am very new to Laravel and I am trying to fully understand how routes work. I want to pass variables through the URL. I understood how I do that, but my problem is a little different:

routes.php

Route::get("/user/{user}", array(
    'as' => 'profile-user',
    'uses' => 'ProfileController@user'
    ));

ProfileController.php

class ProfileController extends BaseController{

public function user($user) {
    $user = User::where('username', '=', Session::get('theuser') );


    if($user->count()) {
        $user = $user->first();

        return View::make('layout.profile')
        ->with('user', $user);

    }

    return App::abort(404);
}


}

In my View, simply:

{{ $user->username }}

Now my problem: This works somewhat, but after the press of a button, this URL will look something like this:

 [this is not a link](http://localhost/tutorial/public/index.php/user/%7Buser%7D)

If I get to edit the URL to something like

 [this is not a link](http://localhost/tutorial/public/index.php/user/Serban)

it does the same thing. But I do not wish to manually edit the URL. How can I get the second URL line without editing?

No correct solution

OTHER TIPS

When you are building your form, pass the user argument like this

Form::open(array('route' => array('route-name-for-update', $user->username)))

You may also use Form Model Binding (read more on the doc):

Form::model($user, array('route' => array('user.update', $user->username)))

Here, user.update is the route name that requires a route defined with this name for update method.

Meanwhile, a more interesting approach for me is to do something like this:

  Route::group(array('prefix' => 'user/{user}'), function()
    {

        Route::get("/{char}", array(
            'as' => 'profile-user',
            'uses' => 'ProfileController@user'));
    }   
    );

Controller:

 public function user($user, $char) {
    $user = User::where('username', '=', Session::get('theuser') );
    $char = Character::where('char_name', '=', 'Cucu' );

    if($user->count()) {
        $user = $user->first();
        $char = $char->first();

        return View::make('layout.profile')
        ->with('user', $user)
        ->with('char', $char);

    }

    return App::abort(404);
}

It doesn't seem for me to be able to do something like this at the push of a button

$logged_user = Session::get('theuser');
return Redirect::route('profile-user', $logged_user);

because I can't put 2 parameters in the Redirect function. This code will get the URL

  [this is not a link](http://localhost/tutorial/public/index.php/user/SerbanSpire)

which obviously doesn't exist The correct URL would be

  [this is not a link]http://localhost/CaughtMiddle/tutorial/public/index.php/user/SerbanSpire/Cucu)

where SerbanSpire is the $user and Cucu is $char. How can I get the correct URL?

When you link to a route you will need to pass the param to the route like so

{{ route('profile-user', 'Serban') }}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top