【问题标题】:Passing variables from route to controller将变量从路由传递到控制器
【发布时间】:2014-02-27 09:50:27
【问题描述】:

我这里有个大问题,不知道怎么解决。

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 个参数重定向到 char-profile-get,但我只需要 2 个。它不会打扰我。如您所见,我使用->with 重定向。我将在视图中打印数据的唯一方法是在视图中执行此操作:

你选择的角色是

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

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

 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

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

我的解决方案可行吗?通过使用->with 重定向并将数据存储在会话中?因为这个解决方案会驱使我大量使用Session::get(),每次我使用数据库中的信息,但我不更新它!如果我错了,请阻止我。

为什么路径中的变量没有转到我的函数的参数?如果我的函数没有任何参数,则代码将相同。基本上,这些参数是没有用的。

【问题讨论】:

标签: php laravel


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你可以试试这个

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

    【讨论】:

    • 它做同样的事情。 URL 正确完成,但 get 路由没有向 getDropDownList 发送任何可用参数。 var_dump($charname);什么都不做,因为这个函数的参数'基本上是空的。
    • 实际上,with 只是将数据闪入session,因此请确保您的会话包含视图中{{ dd(Session::all()) }} 的数据。
    • 另外你的getDropDownList() 不接受任何参数,所以不要传递任何参数。
    • 我已经尝试过了,但我将在我的项目的其余部分基本上使用 Session::get()。我想我再也不会使用普通变量了。这是一个可行的解决方案吗?
    • Session::all() 从会话中返回所有内容,我告诉你通过转储会话来检查会话。
    【解决方案3】:

    如果我理解正确,你只需要

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

    因为你传递了一个空数组。 现在直接在视图中使用 Session::get('username') 和 Session::get('char_name') (layout.profile)

    【讨论】:

    • 公共函数 getDropDownList($username, $char_name) { return View::make('layout.profile') ->with('char_name', $char_name); } 这个函数中的参数仍然是空的
    • 你错了,学习 laravel docs laravel.com/docs/responses。你从空气中得到变量。您不需要方法的参数和 getDropDownList() 上的“with”方法。使用 'with' 方法传递的变量可以直接在视图中使用 Session::get('passed_keyName') 检索,直到您不刷新页面为止。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 2016-03-03
    • 2018-09-09
    • 2017-06-02
    • 2019-12-09
    • 2015-01-06
    相关资源
    最近更新 更多