I have two controllers and I try pass id of variable form method one controller to method to second controller and I got an error like this >MethodNotAllowedHttpException . I will add that my url after the action looks like this >http://localhost/comment?12 . How is the best way of solving this problem ?
You are most probably getting MethodNotAllowedException
, because you are opening a route that is defined as a POST
route via GET
or the other way around.
To avoid that you can use php artisan route:list
and get a list of all defined routes and see how you should "access" them:
+--------+-----------+----------------------------------------------------+------------------------+------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------------------------------+------------------------+------------------------------------------------------------------------+--------------+
| | GET|HEAD | /a/show/{id} | | App\Http\Controllers\AController@show | web |
| | GET|HEAD | /b/show/{id} | | App\Http\Controllers\BController@show | web |
So let's say you have 2 controllers: AController
and BController
. Each of the controllers have a show()
method declared in them.
class AController extends Controller {
//... other AController related code
public function show($id) {
dd($id);
}
}
class BController extends Controller {
//... other BController related code
public function show($id) {
dd($id);
}
}
Then you can define your routes like this:
Route::get('/a/show/{id}', 'AController@show');
Links like: example.com/a/show/10
will "load" AController
's show()
method. All we have in our AController::show()
method's body is dump and die on $id
, we will get 10 printed if we visit that link.
We can replace that dd($id);
with:
redirect()->action('BController@show', ['id' => $id]);
And define another route:
Route::get('/b/show/{id}', 'BController@show');
This way if we open the previous link: example.com/a/show/10, we will be redirected to: example.com/b/show/10
and BController::show()
method will be executed and it prints the variable using dump and die.
Key points: