找不到 Laravel API 路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55056114/
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
Laravel API Routes Not Found
提问by jeesoon
Im new to API and Vue. Im working on Laravel 5.8 api.php and controllers and views and it only return 404 Not Found.
我是 API 和 Vue 的新手。我正在处理 Laravel 5.8 api.php 以及控制器和视图,它只返回 404 Not Found。
this is what ive tried
这是我试过的
api.php
api.php
Route::group(['middleware' => 'api'], function(){
Route::resource('/dashboard/departments', 'DepartmentsController');
});
Controller
控制器
class DepartmentsController extends Controller
{
public function index()
{
return 'hey';
}
}
Route List
路线清单
GET|HEAD | api/dashboard/departments | departments.index | App\Http\Controllers\DepartmentsController@index | api,auth
i tried accessing it by /127.0.0.1:8000/api/dashboard/departments
and /127.0.0.1:8000/dashboard/departments
but both is not working.
我尝试访问它/127.0.0.1:8000/api/dashboard/departments
,/127.0.0.1:8000/dashboard/departments
但两者都不起作用。
回答by Eliseo
remember than routes declared in api.php
will automatically concat to /api
prefix, btw:
请记住,在中声明的路由api.php
将自动连接到/api
前缀,顺便说一句:
Route::get('/hello', ...)
axios.get('/api/hello')
回答by Alec Gordon
Your API routes are within the api
middleware which requires authentication of type API. If you check out the API Authenticationdocumentation you need to have API tokens set up and passed in with your request.
您的 API 路由位于api
需要 API 类型身份验证的中间件中。如果您查看API 身份验证文档,您需要设置 API 令牌并随请求传入。
You either need to pass the token in with your request, remove the api
middleware and have your API routes be unauthenticated, or move the routes that you need to access via browser out of the api
middleware and into the web
middleware and routes file.
您需要将令牌与您的请求一起传递,删除api
中间件并让您的 API 路由未经身份验证,或者将您需要通过浏览器访问的路由移出api
中间件并移入web
中间件和路由文件。
回答by jeesoon
For anyone still wondering, or it just me. This is what i did after many trials.
对于任何仍然想知道的人,或者只是我。这是我经过多次试验后所做的。
i remove the route::group from my API.php and the prefix('api') from RouteServiceProvider.php and replace it with middleware('web')
我从我的 API.php 中删除了 route::group,从 RouteServiceProvider.php 中删除了前缀('api'),并将其替换为中间件('web')
this is my RouteServiceProvider.phpfile
这是我的RouteServiceProvider.php文件
protected function mapApiRoutes()
{
Route::middleware('api')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
and this is my api.phpfile
这是我的api.php文件
Route::resource('/dashboard/departments', 'DepartmentsController');
Route::resource('/dashboard/departments', 'DepartmentsController');
回答by Paras Raiyani
Just add public in url before api.
只需在 api 之前的 url 中添加 public 即可。
Like
喜欢
/127.0.0.1:8000/public/api/dashboard/departments