chimit's avatar

Laravel Echo in Lumen: Call to undefined method routesAreCached()

Hi!

I'm trying to use broadcasting in Lumen. Everything works without any problems except private channels. I can't setup auth routes.

I copied a app\Providers\BroadcastServiceProvider.php file from Laravel and put it into the same place in Lumen. I also added:

$app->register(App\Providers\BroadcastServiceProvider::class);

into the bootstrap\app.php

After that, I got an error:

[2018-03-16 11:06:12] lumen.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Laravel\Lumen\Application::routesAreCached() in /home/vagrant/Code/project/vendor/illuminate/broadcasting/BroadcastManager.php:61
Stack trace:
#0 /home/vagrant/Code/project/vendor/illuminate/support/Facades/Facade.php(221): Illuminate\Broadcasting\BroadcastManager->routes()
#1 /home/vagrant/Code/project/app/Providers/BroadcastServiceProvider.php(17): Illuminate\Support\Facades\Facade::__callStatic('routes', Array)
#2 [internal function]: App\Providers\BroadcastServiceProvider->boot()
#3 /home/vagrant/Code/project/vendor/illuminate/container/BoundMethod.php(29): call_user_func_array(Array, Array)
#4 /home/vagrant/Code/project/vendor/illuminate/container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#5 /home/vagrant/Code/project/vendor/illuminate/container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Laravel\Lumen\Application), Array, Object(Closure))
#6 /home/vagrant/Code/project/vendor/illuminate/container/Container.php(549): Illuminate\Container\BoundMethod::call(Object(Laravel\Lumen\Application), Array, Array, NULL)
#7 /home/vagrant/Code/project/vendor/laravel/lumen-framework/src/Application.php(197): Illuminate\Container\Container->call(Array)
#8 /home/vagrant/Code/project/bootstrap/app.php(92): Laravel\Lumen\Application->register(Object(App\Providers\BroadcastServiceProvider))
#9 /home/vagrant/Code/project/public/index.php(14): require('/home/vagrant/C...')
#10 {main}  

0 likes
6 replies
bobbybouwmann's avatar

The routes helper method for the broadcasting is not supported in Lumen. It calls some methods, including routesAreCached, which are Laravel specific for route caching.

Instead you need to register the routes yourself in your app.php

$this->app['router']->group(['middleware' => ['web']], function ($router) {
    $router->post('/broadcasting/auth', '\' . BroadcastController::class . '@authenticate');
});

Note: this is tweaked example code from the service provider from laravel. I haven't tested this myself!

chimit's avatar

Thanks for the answer, @bobbybouwmann!

But I can't make it work. The app.php has:

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/
$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__.'/../routes/web.php';
});
return $app;

If I put a new route inside it it returns class now found errors.

This doesn't work too:

$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__.'/../routes/web.php';
});

$app->router->group(['middleware' => ['web']], function ($router) {
    $router->post('/broadcasting/auth', Illuminate\Broadcasting\BroadcastController::class . '@authenticate');
});

return $app;

Log:

ReflectionException: Class web does not exist in /home/vagrant/Code/project/vendor/illuminate/container/Container.php:752

I also tried to put

$router->post('/broadcasting/auth', Illuminate\Broadcasting\BroadcastController::class . '@authenticate');

into my routes/web.php, but without success.

bobbybouwmann's avatar

Aah this is just the example from Laravel. You need to add your own middleware group to it. Web is default for laravel, but might not be default in Lumen.

chimit's avatar

@bobbybouwmann, web is default in Lumen too.

It seems to me that these auth routes require sessions and can't work statelessly. So I'm afraid the only way to make it work - implement these routes manually using in my case Pusher PHP SDK directly. I would be glad to see the way how to make it work using the default Lumen tools. Because currently, I'm stuck :)

bobbybouwmann's avatar

Well you can switch back to Laravel and you wouldn't have any issues anymore, but that might not be an option for you.

I don't use Lumen often so it would take me a lot of time to figure it out as well ;)

Please or to participate in this conversation.