Laravel将变量从控制器传递到另一个控制器

时间:2014-07-30 03:03:12

标签: laravel laravel-4

我正在尝试将变量从控制器传递到我尝试使用

的另一个控制器
Redirect::to('dashboard/'.$ssid.'/')->with(compact('wname'))

但不知道我怎么能实现这个目标?

这是我的代码

路线

Route::get('dashboard/{ssid}/', 'HomeController@showDash');

的LoginController

public function post_index()
  {

  if(Auth::attempt($credentials)){
    $users = User::where('username','=',$email)->get();
      foreach ($users as $value):
        $activated = $value['a_status'];
        $wname = $value['wholename'];

      endforeach;

      if($activated == 1):
          $red= Redirect::to('dashboard/'.$ssid.'/')->with(compact('wholename')); 
      else:
          $red= View::make('login');
      endif;
      return $red;
}

 }

的HomeController

public function showDash($ssid,$wholename)
{
 foreach ($wholename as $userVal):
        $fn = $userVal['firstname'];
        $ln = $userVal['lastname'];
     endforeach;
return View::make('dashboard')->with(compact('fn'));

}

根据laravel的调试器,我遇到的错误是Missing argument 2 for HomeController::showDash() ..

1 个答案:

答案 0 :(得分:1)

根据评论更新了答案:

public function post_index()
{

    if(Auth::attempt($credentials)){
    $users = User::where('username','=',$email)->get();
    foreach ($users as $value){
        $activated = $value['a_status'];
        $wname = $value['wholename'];
    }
    if($activated == 1) {
        Redirect::to('dashboard/'.$ssid.'/')->with(['wholename' => $wholename]); 
    }

    return View::make('login');
}


public function showDash($ssid)
{
    $wholename = (Session::has('wholename')) ? Session::get('wholename') : [];
    foreach ($wholename as $userVal) {
        $fn = $userVal['firstname'];
        $ln = $userVal['lastname'];
    }
    return View::make('dashboard')->with(compact('fn'));
}

其他一切都可以保持原样。

更新:修复了“整个名称”中的错误空格(自动更正了,抱歉)。