Serban Spirescu
Serban Spirescu

Reputation: 139

Passing variables from route to controller

I have a big issue over here and I have no idea how to solve it.

routes.php:

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

    Route::post('/{user}/{char_name}/selectedok', array(
        'as' => 'char-profile-post',
        'uses' => 'ProfileController@postDropDownList'
    ));   

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

});

ProfileController.php:

public function getDropDownList() {  

    return View::make('layout.profile');     

}



public function postDropDownList($user, $char_name) {

    if (Auth::check())
    {   
        $user = Auth::user();
        $selected_char = Input::get('choose');
        $char_name = User::find($user->id)->characters()->where('char_name', '=', $selected_char)->first();


        return Redirect::route('char-profile-get', array($user->username, $char_name->char_dynasty, $char_name->char_name))
                ->with('user', $user->username)
                ->with('charname', $char_name->char_name)
                ->with('dynastyname', $char_name->char_dynasty);



    }
}

Yes I know I redirect to char-profile-get with 3 parametres, but I only need 2. It doesn't bother me. As you observe, I redirect using ->with. The only way I will print the data in the view is by doing this in the view:

Your chosen character is

{{ Session::get('charname') }} {{ Session::get('dynastyname')}}

If I don't overwrite the variables in postDropDownList($user, $char_name) my URL will literally look like this:

 http://localhost/CaughtMiddle/tutorial/public/index.php/user/%7Buser%7D/%7Bchar_name%7D/selectedok

instead of this:

 http://localhost/CaughtMiddle/tutorial/public/index.php/user/SerbanSpire/Spirescu/selectedok?Serban

So the verdict. The parameters in the functions will never receive any data. I don't even know how to describe them, the functions' parameters are null, even if I redirect to the get route with the correct data. var_dump($user) or var_dump($char_name) doesn't print anything on the browser, dd($user) and dd($char_name) also doesn't print anything nor does print_r().

Is my solution a viable one? By redirecting using ->with and storing the data in Session? Because this solution will drive me into using Session::get() a lot, every time I use information from the database, but i don't update it! Stop me if I am wrong.

Why aren't the variable's from the route going to the parameters of my functions? If my functions wouldn't have any parameters, the code would work the same. Basically, those parameters are there for nothing.

Upvotes: 1

Views: 202

Answers (3)

Serban Spirescu
Serban Spirescu

Reputation: 139

I have finally managed to resolve my issue. I have made a new CharacterController.php file and I put the get function inside the new controller. This way the function's parameters will successfully arrive and be useful.

Upvotes: 1

TeroBlaZe
TeroBlaZe

Reputation: 75

If i understand correct you just need

        return Redirect::route('char-profile-get', array(
        'username' => $user->username,
        'char_name' => $char_name->char_name))
        ->with('user', $user->username)
        ->with('charname', $char_name->char_name)
        ->with('dynastyname', $char_name->char_dynasty);

because you pass an empty array. now use Session::get('username') and Session::get('char_name') directly in the view (layout.profile)

Upvotes: 0

The Alpha
The Alpha

Reputation: 146191

You may try this

$user = Auth::user();
$char = $user->characters()->where('char_name', Input::get('choose'))->first();

return Redirect::route('char-profile-get')
               ->with('user', $user->username)
               ->with('charname', $char->char_name)
               ->with('dynastyname', $char->char_dynasty);

Upvotes: 0

Related Questions