laravel 4 重定向到带有 2 个参数的路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23275063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:24:57  来源:igfitidea点击:

laravel 4 Redirect to route with 2 parameters

phplaravelroutinglaravel-4

提问by user3359775

I'm currently trying to implement redirects using

我目前正在尝试使用

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('/');
}

The parameters in Redirect aren't working. Store is a POST method in ForumController. The parameters that Store received are OK because I don't have problems with validation 'if'. I've could created a forum and save it, but when I try to redirect I have this error

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

Trying to get property of non-object

And users.courses.forums.index is the name of my URI with Action ForumController@index. This last method needs 2 parameters ($username,$courseid). Like this

users.courses.forums.index 是我的 URI 名称,带有 Action ForumController@index。最后一个方法需要 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'));    
}

回答by msturdy

Why not use Redirect::route()directly and pass your variables as an array?

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

Something like this should work...

像这样的事情应该工作......

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

回答by ahmed hamdy

There two ways

有两种方式

1] you can use Redirect::route()like @msturdy answer

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

EX:

前任:

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

2] you can also use Redirect::action()

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

EX:

例如

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

Like lavarel Documentation for redirects

lavarel 重定向文档