Php 将变量从路由传递到控制器

Php 将变量从路由传递到控制器,php,laravel,Php,Laravel,我这里有个大问题,我不知道怎么解决 routes.php: Route::group(array('prefix' => 'user'), function() { Route::post('/{user}/{char_name}/selectedok', array( 'as' => 'char-profile-post', 'uses' => 'ProfileController@postDropDownList' ));

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

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

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



    }
}
ProfileController.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'
    ));

});
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\u name)
my
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\u dump($user)
var\u dump($char\u name)
不在浏览器上打印任何内容,
dd($user)
dd($char\u name)
也不打印任何内容,也不
print\u r()

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


为什么路径中的变量没有传递到函数的参数?如果我的函数没有任何参数,那么代码的工作原理也是一样的。基本上,这些参数是免费的。

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

        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);
因为你传递了一个空数组。 现在直接在视图(layout.profile)中使用Session::get('username')和Session::get('char_name')。

您可以尝试以下方法

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

我终于设法解决了我的问题。我创建了一个新的CharacterController.php文件,并将get函数放在新控制器中。通过这种方式,函数的参数将成功到达,并且非常有用

公共函数getDropDownList($username,$char_name){return View::make('layout.profile')->with('char_name',$char_name);}此函数中的参数仍然为空。如果您错了,请学习laravel文档。你从空气中得到了变化。您不需要getDropDownList()上的方法参数和“with”方法。使用“with”方法传递的变量可以通过Session::get(“passed_keyName”)直接在视图中检索,直到您不刷新页面为止。它也会执行相同的操作。URL已正确完成,但get路由不会向getDropDownList发送任何可用参数。var_dump($charname);不做任何事情,因为此函数的参数“基本上为空。实际上,
with
只是将数据闪烁到
session
中,因此请确保您的会话包含视图中
{{dd(session::all())}}
中的数据。此外,您的
getDropDownList()
不接受任何参数,因此不要传递任何参数。我已经尝试过了,但我将在项目的其余部分基本上使用Session::get()。我想我再也不会使用正态变量了。这是一个可行的解决方案吗?
Session::all()
返回Session中的所有内容,我告诉过您通过转储Session来检查Session