【问题标题】:laravel 4 Redirect to route with 2 parameterslaravel 4重定向到带有2个参数的路由
【发布时间】:2014-06-10 02:27:09
【问题描述】:

我目前正在尝试使用实现重定向

public function store($username,$courseId)
{       
        if (Auth::user()->username == $username && Auth::user()->courses()->where('id', '=', $courseId)->first() != null){
        $course = Course::find($courseId);
        $forum = new Forum();
        $forum->description = Input::get('description');
        $forum->course_id   = Input::get('course_id');
        $forum->save();
        return Redirect::to(route('users.courses.forums.index',Auth::user()->username,$course->id));
        }
    return Redirect::to('/');
}

重定向中的参数不起作用。 Store 是 ForumController 中的一个 POST 方法。 Store 收到的参数没问题,因为我没有验证“if”的问题。我可以创建一个论坛并保存它,但是当我尝试重定向时出现此错误

Trying to get property of non-object

users.courses.forums.index 是我的带有 Action ForumController@index 的 URI 的名称。最后一种方法需要 2 个参数($username,$courseid)。像这样

public function index($username,$courseId)
{       
        $course = Course::find($courseId);
        $forum = DB::table('forums')->where('course_id',$course->id)->get();
        return View::make('forums.index',compact('course','forum'));    
}

【问题讨论】:

    标签: php laravel routing laravel-4


    【解决方案1】:

    为什么不直接使用Redirect::route() 并将变量作为数组传递?

    这样的东西应该可以工作......

        return Redirect::route('users.courses.forums.index', 
                                array(Auth::user()->username, $course->id));
    

    【讨论】:

      【解决方案2】:

      有两种方法

      1] 你可以使用Redirect::route() 像@msturdy 回答

      前:

      return Redirect::route('users.courses.forums.index',array(Auth::user()->username, $course->id));
      

      2]你也可以使用Redirect::action()

      EX

      return Redirect::action('ForumController@index',array(Auth::user()->username, $course->id));
      

      点赞 lavarel Documentation for redirects

      【讨论】:

        猜你喜欢
        • 2013-09-14
        • 2015-11-06
        • 1970-01-01
        • 1970-01-01
        • 2017-12-30
        • 2020-02-13
        • 2016-04-07
        • 2020-10-10
        • 2015-11-07
        相关资源
        最近更新 更多