Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

birdietorerik's avatar

Get error -> Route [player] not defined

Have a error in my middelware -> player

Symfony\Component\Routing\Exception\RouteNotFoundException
Route [player] not defined.

My middelware

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class Dashboard
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
       
        if (!Auth::check()) {
            return redirect()->route('login');
        }
        
        //$currentrole = Auth()->user()->roles->map->title;
        
        $currentrole = 'player';
        if ($currentrole == 'Superadmin') {
            return $next($request);
        }

        return redirect()->route('player');

    }
}

My Route -> API

<?php

Route::group(['prefix' => 'v1', 'as' => 'api.', 'namespace' => 'Api\V1\Admin', 'middleware' => ['auth:sanctum']], function () {
    // Abilities
    Route::apiResource('abilities', 'AbilitiesController', ['only' => ['index']]);

    // Locales
    Route::get('locales/languages', 'LocalesController@languages')->name('locales.languages');
    Route::get('locales/messages', 'LocalesController@messages')->name('locales.messages');

    // Permissions
    Route::resource('permissions', 'PermissionsApiController');
    
    // Roles
    Route::resource('roles', 'RolesApiController');

    // Users
    Route::resource('users', 'UsersApiController');

    // Content Category
    Route::resource('content-categories', 'ContentCategoryApiController');

    // Content Tag
    Route::resource('content-tags', 'ContentTagApiController');

    // Content Page
    Route::post('content-pages/media', 'ContentPageApiController@storeMedia')->name('content-pages.storeMedia');
    Route::resource('content-pages', 'ContentPageApiController');

    // Country
    Route::resource('countries', 'CountryApiController');

    // Golfclub
    Route::resource('golfclubs', 'GolfclubApiController');

    // Grad
    Route::resource('grads', 'GradApiController');

    // Dommereb
    Route::resource('dommerebs', 'DommerebApiController');

    // Startliste
    Route::resource('startlistes', 'StartlisteApiController');

    // Lokaleregler
    Route::resource('lokalereglers', 'LokalereglerApiController');

    // Walkie Talkie
    Route::apiResource('walkie-talkies', 'WalkieTalkieApiController', ['only' => ['index']]);
    // Location GPS
    Route::apiResource('places', 'LocationApiController', ['only' => ['index']]);
    // Chat
    Route::apiResource('chats', 'ChatApiController', ['only' => ['index']]);

    // Settings
    Route::apiResource('settings', 'SettingsApiController', ['only' => ['index']]);

    // Testing
    Route::apiResource('testings', 'TestingApiController', ['only' => ['index']]);


    // Turnements
    Route::resource('turnements', 'TurnementsApiController');

     // Flightdata
    Route::resource('Flightdata', 'FlightdataApiController');

     // Gpstrackerclub
    Route::resource('gpstrackerclubs', 'GpstrackerclubApiController');

    // Testing
    Route::apiResource('timeusedflights', 'TimeusedflightsApiController', ['only' => ['index']]);

    // Golfclubfeedback
    Route::resource('golfclubfeedbacks', 'GolfclubfeedbackApiController');

    // Investor
    Route::post('investors/media', 'InvestorApiController@storeMedia')->name('investors.storeMedia');
    Route::resource('investors', 'InvestorApiController');

    // Profile Index
    //
    Route::apiResource('profilen', 'ProfilesApiController', ['only' => ['index']]);
    //Route::resource('profilen', 'ProfilesApiController');

    //Route::resource('Timer','Timetracking');

     // Configuration
    Route::resource('configurations', 'ConfigurationApiController');
     
    // Gpstracker
    //Route::resource('gpstrackers', 'GpstrackerApiController');
    Route::get('gpstrackers', 'GpstrackerApiController@gpstrackers')->name('gpstrackers');
    Route::get('gpsdistence', 'GpstrackerApiController@gpsdistence')->name('gpsdistence');
    Route::get('clubconfig', 'GpstrackerApiController@clubconfig')->name('clubconfig');
    Route::get('gpsholes', 'GpstrackerApiController@gpsholes')->name('gpsholes');
    Route::get('getconfigdata' , 'ConfigurationApiController@getconfigdata')->name('getconfigdata');
    Route::get('storegpspos', 'GpstrackerApiController@storegpspos')->name('storegpspos');
});

Route::group(['prefix' => 'v1', 'as' => 'api.', 'namespace' => 'Api\V1\Admin'], function () {

     
    // Dashboard - dashboard
    Route::get('dashboard', 'DashboardApiController@index')->name('dashboard')->middleware('dashboard');
    
    // Dashboard - Player
    Route::get('player', 'PlayerApiController@index')->name('player')->middleware('player');

});    

My question is why it dosent find route -> player ?

0 likes
14 replies
MichalOravec's avatar

You have api.player named route and not just player.

You are using old route syntax and as in group is prefix for route name.

birdietorerik's avatar

@MichalOravec Hi!

Try to change to:

return redirect()->route('api.player');

Same error ?

Even try to change to:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class Dashboard
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
       
        if (!Auth::check()) {
            return redirect()->route('login');
        }
        
        //$currentrole = Auth()->user()->roles->map->title;
        
        $currentrole = 'player';
        if ($currentrole == 'Superadmin') {
            return $next($request);
        }
        
        abort(403, 'Access denied');

    }
}

Strange, still get same error ???

birdietorerik's avatar

Hi!

Maby somthing here, my web route

?php

Route::redirect('/', '/login');

// Check if users is online or offline
Route::get('/check', 'UserController@userOnlineStatus');

Auth::routes(['register' => false]);

Route::group([
    'prefix'     => 'admin',
    'as'         => 'admin.',
    'namespace'  => 'Admin',
    'middleware' => ['auth'],
], function () {
    Route::view('/{any?}', 'layouts.admin.app')->name('dashboard')->where('any', '.*');
});

Route::get('/roller','Auth\SelectroleController@index')->name('roller');
Route::post('/roller','Auth\SelectroleController@selectrole')->name('roller');

// Dashboards
// Dasboard - superadmin

// Dashboard - Federation
Route::get('federation', 'FederationApiController@index')->name('federation')->middleware('federation');
Route::get('/error','Auth\GetErrorController@index')->name('error');

birdietorerik's avatar

Hi all!

Error is gone, (cash problem) . So it dosent show error But it dosent redirect to route ???

return redirect()->route('api.player');

Sinnbeck's avatar

@birdietorerik don't cache in dev.. It would help if you could perhaps explain what happens instead. And be sure that you have checked with network in your browser if it actually hits the route but redirects you to somewhere else. I recall you had middleware

Btw.. I am curious. Why would this ever redirect if it's an api?

birdietorerik's avatar

@Sinnbeck Hi!

Checked network, and its call redirect...

Request URL: http://portal.test:8000/api/v1/dashboard
Request Method: GET
Status Code: 302 Found
Remote Address: 127.0.0.1:8000
Referrer Policy: strict-origin-when-cross-origin

It supose to call redirect

http://portal.test:8000/api/v1/player

This is so strange ?

How to remove cahing in dev ?

Snapey's avatar

@birdietorerik why redirect an API request? It makes no sense.

The headers in the 302 response will provide the URI you are being redirected to.

Bear in mind that a redirect is the server telling the client to try again at a different URL. The server does not send back the content from the redirected page. The client has to ask for it.

To be clear, you request api/v1/dashboard. The server replies with 302 message and the URI of a different route to try.

birdietorerik's avatar

@Snapey Hi! What i want, is that user isent allowed to visitt example dashboard. I know that using abort(403) insted of redirect is best. But this only work when page is a blade file. I am using VUE.js. Do you have a tip of how to do this ? Display error page using VUE ?

Snapey's avatar

@birdietorerik If its an API then you should a) return an unauthorised response, or b) return a redirect. Bear in mind that in both cases, you have to write a handler for the error in your Vue code. Redirects are handled CLIENT side, not server.

Snapey's avatar

@birdietorerik By the way. Why are you allowing your user to access an invalid route from within your vue code?

Please or to participate in this conversation.