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 ?
1条回答 默认 最新
- dongyin6576 2018-12-25 14:03关注
You are most probably getting
MethodNotAllowedException
, because you are opening a route that is defined as aPOST
route viaGET
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
andBController
. Each of the controllers have ashow()
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
'sshow()
method. All we have in ourAController::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
andBController::show()
method will be executed and it prints the variable using dump and die.Key points:
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 虚幻5 UE美术毛发渲染
- ¥15 CVRP 图论 物流运输优化
- ¥15 Tableau online 嵌入ppt失败
- ¥100 支付宝网页转账系统不识别账号
- ¥15 基于单片机的靶位控制系统
- ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
- ¥15 下图接收小电路,谁知道原理
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度