[英]using Laravel 7 Api routes
I'm trying to use simple laravel api for getting and sending requests, after define this api routes in api.php
: I'm trying to use simple laravel api for getting and sending requests, after define this api routes in
api.php
:
Route::prefix('Api/v1')->group(function () {
Route::any('login', 'Api\v1\AuthController@login');
Route::any('register', 'Api\v1\AuthController@register');
});
and creating AuthController in app/http/controller/Api/v1
directory:并在
app/http/controller/Api/v1
目录中创建 AuthController:
class AuthController extends Controller
{
public function login()
{
dd(request()->all());
}
public function register()
{
dd(request()->all());
}
}
i get 404
error on this link:我在此链接上收到
404
错误:
http://127.0.0.1:8000/Api/v1/login
how can i resolve this problem?我该如何解决这个问题?
Routes in api.php
are automatically prefixed with /api
. api.php
中的路由自动以/api
为前缀。 Currently, your routes are:目前,您的路线是:
http://127.0.0.1:8000/api/Api/v1/login
http://127.0.0.1:8000/api/Api/v1/register
So navigating to http://127.0.0.1:8000/Api/v1/login
is a 404.所以导航到
http://127.0.0.1:8000/Api/v1/login
是一个 404。
If you remove /Api
, and just use Route::prefix('/v1') ...
then you should have no issue.如果您删除
/Api
,并只使用Route::prefix('/v1') ...
那么您应该没有问题。
Also, always double check your routes with php artisan route:list
to see what's wrong.另外,请务必使用
php artisan route:list
仔细检查您的路线,看看有什么问题。
The API Routes are already prefixed by /api . API 路由已经以 /api 为前缀。 I think the correct structure you'd looking for would be
我认为您要寻找的正确结构是
Route::prefix('v1')->group(function () {
Route::any('login', 'AuthController@login');
Route::any('register', 'AuthController@register');
});
This way, you're calling the methods Login and Register from you /Controllers/AuthController file with the route这样,您就可以使用路由从 /Controllers/AuthController 文件中调用 Login 和 Register 方法
http://127.0.0.1:8000/api/v1/login http://127.0.0.1:8000/api/v1/login
You can use many ways to define routes for API in laraval > routes > api.php file.您可以使用多种方法在 laraval > routes > api.php 文件中定义 API 的路由。
In this i'm going to explain how we can use routes group in the laraval..在这篇文章中,我将解释我们如何在 laraval 中使用路由组。
Route::group([
'namespace' => 'Customers', //namespace App\Http\Controllers\Customers;
'middleware' => 'auth:api', // this is for check user is logged in or authenticated user
'prefix' => 'customers' // you can use custom prefix for your rote {{host}}/api/customers/
], function ($router) {
// add and delete customer groups
Route::get('/', [CustomerController::class, 'index']); // {{host}}/api/customers/ this is called to index method in CustomerController.php
Route::post('/create', [CustomerController::class, 'create']); // {{host}}/api/customers/create this is called to create method in CustomerController.php
Route::post('/show/{id}', [CustomerController::class, 'show']); // {{host}}/api/customers/show/10 this is called to show method in CustomerController.php parsing id to get single data
Route::post('/delete/{id}', [CustomerController::class, 'delete']); // {{host}}/api/customers/delete/10 this is called to delete method in CustomerController.php for delete single data
});
You can create controller using artisan command with default methods您可以使用 artisan 命令和默认方法创建 controller
php artisan make:controller Customers/CustomerController --resource
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.