简体   繁体   English

将变量从路径传递到控制器

[英]Passing variables from route to controller

I have a big issue over here and I have no idea how to solve it. 我这里有一个大问题,我不知道如何解决。

routes.php: 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: 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. 是的,我知道我使用3个参数重定向到char-profile-get ,但是我只需要2个参数。这不会打扰我。 As you observe, I redirect using ->with . 如您所见,我使用->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: 如果我不覆盖postDropDownList($user, $char_name)的变量, postDropDownList($user, $char_name)我的URL会像这样:

 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. 我什至不知道如何描述它们,函数的参数为​​null,即使我使用正确的数据重定向到get路由。 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() . var_dump($user)var_dump($char_name)在浏览器上不显示任何内容, dd($user)dd($char_name)也不显示任何内容,也不显示print_r()

Is my solution a viable one? 我的解决方案可行吗? By redirecting using ->with and storing the data in Session? 通过使用->with重定向并将数据存储在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! 因为此解决方案将驱使我大量使用Session::get() ,所以每次我使用数据库中的信息时都不会更新它! 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. 基本上,这些参数毫无用处。

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. 我已经创建了一个新的CharacterController.php文件,并将get函数放入了新控制器中。 This way the function's parameters will successfully arrive and be useful. 这样,函数的参数将成功到达并很有用。

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) 现在直接在视图(layout.profile)中使用Session :: get('username')和Session :: get('char_name')

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM