将变量从路径传递到控制器 [英] Passing variables from route to controller

查看:49
本文介绍了将变量从路径传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有个大问题,我不知道如何解决.

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



    }
}

是的,我知道我重定向到具有3个参数的 profile-get ,但是我只需要2个参数.这不会打扰我.如您所见,我使用->和进行重定向.我将在视图中打印数据的唯一方法是在视图中执行以下操作:

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:

您选择的字符是

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

如果我不覆盖 postDropDownList($ user,$ char_name)中的变量,那么我的 URL 看起来将像这样:

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

代替此:

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

所以判决.函数中的参数将永远不会接收任何数据.我什至不知道如何描述它们,函数的参数为​​null,即使我使用正确的数据重定向到get路由. var_dump($ user) var_dump($ char_name)在浏览器上不打印任何内容, dd($ user)dd($ char_name)也不会打印任何内容,也不会打印 print_r().

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().

我的解决方案是否可行?通过使用-> 进行重定向并将数据存储在Session中?因为此解决方案将驱使我大量使用 Session :: get(),所以每次我使用数据库中的信息时都不会更新它!如果我错了就阻止我.

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.

推荐答案

我终于设法解决了我的问题.我已经创建了一个新的CharacterController.php文件,并将get函数放入了新控制器中.这样,函数的参数将成功到达并很有用.

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.

这篇关于将变量从路径传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆