简体   繁体   中英

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 :

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:

class AuthController extends Controller
{
    public function login()
    {
        dd(request()->all());
    }

    public function register()
    {
        dd(request()->all());
    }
}

i get 404 error on this link:

http://127.0.0.1:8000/Api/v1/login

how can i resolve this problem?

Routes in api.php are automatically prefixed with /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.

If you remove /Api , and just use Route::prefix('/v1') ... then you should have no issue.

Also, always double check your routes with php artisan route:list to see what's wrong.

The API Routes are already prefixed by /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

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.

In this i'm going to explain how we can use routes group in the 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

php artisan make:controller Customers/CustomerController --resource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM