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

dcranmer's avatar

Method prefix does not exist (?)

I've upgraded from 5.2 to 5.3, and I'm just stumped. Followed all of the instructions. Ran composer autoload -o. But I can't get past this exception:

BadMethodCallException in Macroable.php line 74:
Method prefix does not exist.

Running php artisan route:list gives me the same error:

[BadMethodCallException]       
Method prefix does not exist

It's got something to do with the RouteServiceProvider and/or the Route facade. Here are the top few lines of the exception page:

in Macroable.php line 74
at Router->__call('prefix', array('api')) in Facade.php line 237
at Router->prefix('api') in Facade.php line 237
at Facade::__callStatic('prefix', array('api')) in    RouteServiceProvider.php line 69
at Route::prefix('api') in RouteServiceProvider.php line 69
at RouteServiceProvider->mapApiRoutes() in  RouteServiceProvider.php line 39
at RouteServiceProvider->map()

I've googled and haven't found a cause or solution, although a couple of people seem to be running into the same thing. Any ideas would be greatly appreciated.

0 likes
12 replies
Jaytee's avatar

@mesqueeb This is the same error you were stuck on right?

You see whats weird about this is that fluent routing wasn't introduced until 5.4 so i'm pretty sure you couldn't define a prefix() method in previous versions.

If i'm correct then for some reason, 5.4 features are being pulled in.

You could change the RouteServiceProvider and remove the prefix method and just define the prefix as you usually would on a route group and see if that works.

Route::group(['prefix' => 'api'], function () {
    // require routes.
});
1 like
dcranmer's avatar

Thanks for responding. I'm not terribly fluent in routing, and don't this business with route groups. I used to have one reasonably straightforward routes file that took care of my needs. Part of me almost wants to continue the upgrade to 5.4. But I thought I'd stop for a bit at 5.3 and make sure everything is working before moving on. I'll try your suggestion out if I can get the syntax right.

dcranmer's avatar

Grrr... the more I try to fix it, the leaks get sprung. The errors keep changing, but there's always a new one. Feels like I'm going down a rabbit hole. The latest one is [Symfony\Component\Debug\Exception\FatalErrorException] Trait 'Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers' not found

Here's my edited mapWebRoutes method:

  protected function mapWebRoutes()

{ Route::group(['middleware' => 'web', 'namespace' => $this->namespace], function () { require base_path('routes/web.php'); }); }

I'm sure that this isn't right. but I'm starting to run out of patience with this.

hendranucleo's avatar

As i know there is a ($router) within the function in RouteServiceProvider in Laravel 5.3. Have you try,

Route::group([
  'middleware' => 'web',
  'namespace' => $this->namespace,
  ], function ($router) {
       require base_path('routes/web.php');
});
dcranmer's avatar

Just to follow up, using @hendranucleo's suggestion got me rolling (thank you!). It was still no picnic after that, as there are a number of changes not mentioned in the upgrade instructions that I had to account for. It took most of a day to go through each new error and figure out whether a key file had either changed and had to be replaced with a fresh one, or had to be added, etc.

hendranucleo's avatar

Can you post your mapApiRoutes method in RouteServiceProvider ?

dcranmer's avatar
Route::group([
  'middleware' => 'api',
  'namespace' => $this->namespace,
  'prefix' => 'api',
], function ($router) {
  require base_path('routes/api.php');
});
hendranucleo's avatar

Do you have this line after namespace on RouteServiceProvider ?

<?php
namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
hendranucleo's avatar

How you upgrading your laravel? copy paste folder or using composer?

dcranmer's avatar

I used Composer, but, as I wrote previously, I still had to make a number of manual changes. New routes directory, new auth controllers, etc., etc. Everything seems to be fine now, but it took more work than I expected.

llbbl's avatar

Something to do with composer.json. I had this problem also upgrading from 5.3 to 5.4.

Try renaming current composer.json to something else; copy in a vanilla one from 5.3; run composer update; then copy in your own additions to the require and require-dev section. That fixed it for me.

Please or to participate in this conversation.