Monday, April 10, 2017

Laravel 5.2 bind controllers to abstract route

I am upgrading application written in Laravel 4.2 to Laravel 5.2. I have long namespaces for my controllers and want to shorten them.

For example:

CustomServiceProvider.php

public function register()
{
    $this->app->bind('Shortname\Somecontroller', 'Really\Really\Long\Name\Somecontroller');
}

routes.php

$router->get('someroute', ['uses' => 'Shortname\Somecontroller@someFunction']);

was possible inside custom service provider and works in Laravel 4.2 and Larave 5.1. After I've started migrating to Laravel 5.2 it stopped working.

Closest I came to the solution was this issue report: https://github.com/laravel/framework/issues/14920 but it doesn't work either.

This works for example:

routes_alternative.php

$router->get('someroute', ['uses' => 'Really\Really\Long\Name\Somecontroller@someFunction']);

I've tried Really\Really\Long\Name\Somecontroller::class and moving everything to RouteServiceProvider. I've also tried artisan route:clear,artisan cache:clear, artisan dump-autoload, artisan clear-compiled and all sorts of other composer and artisan shenanigans.

All actions result in exception:

ReflectionException in Route.php line 280: Class Shortname\Somecontroller does not exist

Does someone have similar expiriences and was able to solve it?



via boroboris

Advertisement