laravel 前端有?id=12这么一个api请求,请教路由怎么写

前端请求:http://***.test/api/v1/list?id=17

Route::get('/api/v1/list?id={id}', [\App\Http\Controllers\SignController::class, 'list']);

提示这样的错误

Oops! An Error Occurred
The server returned a "405 Method Not Allowed".
Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 5

ilst 后面删掉 用方法中加参数 ,function test(Request $reques).用 $request->get('id'); 当然还有其他方法我懒得打字

3年前 评论

405就是你的请求方式错了,接口定义的是 get 请求方式看看是不是也是 get 如果不一样就会报405。

可以用路由参数传递 ID,依赖注入

Route::get('/api/v1/list/{id}', [\App\Http\Controllers\SignController::class, 'list']);

方法

public function (Model $model)  // 你的模型
{
    ...
}
3年前 评论
随波逐流

路由参数的定义及获取参数方式有两种

方式一:

// 定义路由
Route::get('/api/v1/list/{id}', [XxxController::class, 'function1']);

// 获取方式
class XxxController {
    function function1($id){
        echo $id;
    }
}

方式二:

// 定义路由
Route::get('/api/v1/list', [XxxController::class, 'function1']);

// 获取方式
class XxxController {
    function function1(Request $request){
        echo $request->input('id');
    }
}

还有一个需要注意的地方 方法名 不要使用 关键词, 例如: list

3年前 评论

先把文档看一遍吧,或者找个入门教程
推荐一个》laravelacademy.org/books/laravel-t...

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!