2014-04-24 16 views
6

tôi đang cố gắng để thực hiện đổi hướng sử dụnglaravel 4 Redirect để định tuyến với 2 thông số

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

Các thông số trong Redirect không làm việc. Store là một phương thức POST trong ForumController. Các tham số mà Store nhận được là OK vì tôi không gặp vấn đề với việc xác nhận 'if'. Tôi đã có thể tạo ra một diễn đàn và lưu nó, nhưng khi tôi cố gắng để chuyển hướng tôi có lỗi này

Trying to get property of non-object 

Và users.courses.forums.index là tên của URI của tôi với hành động ForumController @ index. Phương pháp cuối cùng này cần 2 tham số ($ username, $ courseid). Như thế này

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

Trả lời

7

Tại sao không sử dụng Redirect::route() trực tiếp và chuyển các biến của bạn thành mảng?

Something như thế này nên làm việc ...

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

Có hai cách

1] bạn có thể sử dụng Redirect::route() như câu trả lời @msturdy

EX:

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

2] bạn cũng có thể sử dụng Redirect::action()

EX:

return Redirect::action('[email protected]',array(Auth::user()->username, $course->id)); 

Giống như lavarel Documentation for redirects

Các vấn đề liên quan