Reputation: 3137
I have a simple package and I want to use the controller. When I try to use it in routes I got
Class App\Http\Controllers\Tropicalista\Admin\Controllers\DashboardController
does not exist
I have this in my /routes/web.php
Route::group([
'namespace' => '\Tropicalista\Admin\Controllers',
'prefix'=> 'admin'], function() {
Route::get('/', ['as' => 'admin.root', 'uses' => 'DashboardController@index']);
});
My controller:
namespace Tropicalista\Admin\Controllers;
use Illuminate\Http\Request;
use Analytics;
use Carbon\Carbon;
use Spatie\Analytics\Period;
use Illuminate\Support\Collection;
use Illuminate\Routing\Controller;
class DashboardController extends Controller
{...}
I think is a namespace problem. So how can I call the package controller?
Upvotes: 2
Views: 1484
Reputation: 482
In order to call package controller, change the namespace group of RouteServiceProvider from
protected $namespace = 'App\Http\Controllers';
to null/empty i.e.
protected $namespace = '';
Then, the route can be written as,
Route::get('homepage', 'Package\Namespace\Controllers\ControllerName@ActionName');
Further, if you want to write route for the default controller, use leading slash '/' before starting url.
Route::get('/homepage', 'App\Http\Controllers\ControllerName@ActionName');
Whether it is good practice or not but it solved the problem.
Upvotes: 1
Reputation: 163978
Since it's a package, you need to register the routes in the package.
You can see an example of registering package controllers here:
$routeConfig = [
'namespace' => 'Barryvdh\Debugbar\Controllers',
'prefix' => $this->app['config']->get('debugbar.route_prefix'),
'domain' => $this->app['config']->get('debugbar.route_domain'),
'middleware' => [DebugbarEnabled::class],
];
$this->getRouter()->group($routeConfig, function($router) {
$router->get('open', [
'uses' => 'OpenHandlerController@handle',
'as' => 'debugbar.openhandler',
]);
});
Upvotes: 1
Reputation: 9863
By default, the
RouteServiceProvider
includes your route files within anamespace
group, allowing you to register controller routes without specifying the fullApp\Http\Controllers
namespace prefix. So, youonly
need to specify the portion of the namespace thatcomes after
the baseApp\Http\Controllers
namespace.
You need to remove namespace
Route::group(['prefix'=> 'admin'], function() {
Route::get('/', ['as' => 'admin.root', 'uses' => '\Tropicalista\Admin\Controllers\DashboardController@index']);
});
Upvotes: 5