puna's avatar
Level 1

How to call other routes using RouteServiceProvider

Hello i have a file inside the routes folder called Administration.php and this is its content:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\UsersController;

Route::resource('/users',UsersController::class);

Then i made this to the RouteServiceProvider.php:

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
//            Route::prefix('api')
//                ->middleware('api')
//                ->namespace($this->namespace)
//                ->group(base_path('routes/api.php'));
//
//            Route::middleware('web')
//                ->namespace($this->namespace)
//                ->group(base_path('routes/web.php'));

            $this->loadRoutesFrom(base_path('routes/administration.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

Then at api.php i did this:

<?php

use App\Http\Controllers\Api\RoleController;
use App\Http\Controllers\Auth\LoginController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Providers\RouteServiceProvider;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('/login', [LoginController::class , 'login']);
Route::post('/refreshtoken', 'LoginController.php@refreshToken');

Route::group(['middleware' => 'auth:api'], function () {
    Route::post('/logout',[LoginController::class,'logout']);
    Route::resource('/roles', RoleController::class);


    app()->make(RouteServiceProvider::class)->boot(); //this
});

So when i use this url on Postman http://127.0.0.1:8000/api/users/ i get this error:

"message": "",
    "exception": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
0 likes
3 replies
krisi_gjika's avatar

There is no route /api/users but just /users, also RouteServiceProvider::class should load api.php not the other way around.

krisi_gjika's avatar

@puna try

 /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

			Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/administration.php'));
        });
    }

Please or to participate in this conversation.