Reputation: 759
I want to have controllers in my Laravel 4 package, but I can't get the routing to work.
I've followed the package instructions in the Laravel 4 documentation, and got the routes.php file working with non-controller routes.
Could someone please give me some instructions on how to get package controllers to work in Laravel 4, it would be very much appreciated.
Thanks in advance.
Lars
// EDIT:
// routes.php
Route::get('admin', 'Package::AdminController@index'); // Does not work
Route::get('admin', function(){ // Works fine
return 'Dashboard';
})
Upvotes: 5
Views: 5995
Reputation: 2418
You'll need to reference the Controller with it's Namespace too
Route::get('/admin', 'PackageNS\Package\Controllers\AdminController@getIndex');
or even
Route::controller('PackageNS\Package\Controllers\AdminController', 'admin');
Upvotes: 3
Reputation: 407
I don't know the specifics of your situation, nor do I know if this is the "proper" way to fix this issue, but since I came across the same problem I figured I'd share how I solved it.
I put my package controllers in the controllers subdirectory, so that my directory structure looks like this:
/src
/Vendor
/Package
PackageServiceProvider.php
/config
/controllers
/lang
/migrations
/views
/tests
/public
Then, I added the controllers folder to my package's composer.json autoload class map.
{
"name": "kevin-s-perrine/my-first-packge",
"description": "",
"authors": [
{
"name": "Kevin S. Perrine",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.0.x"
},
"autoload": {
"classmap": [
"src/migrations",
"src/controllers"
],
"psr-0": {
"KevinSPerrine\\MyFirstPackage": "src/"
}
},
"minimum-stability": "dev"
}
Finally, I ran composer dump-autoload
in the package's root directory, and then reference the controller by name in the routes file.
Route::get('myfirstpackage', 'MyFirstPackageHomeController@getIndex');
Upvotes: 10
Reputation: 1638
Did you do this:
composer dump-autoload
The autoloader needs to be told about those shiny new classes. I also suggest you check the webserver logs for errors.
Upvotes: -1
Reputation: 4888
In your package's service provider, have you included your routes file? I don't believe L4 loads the route file automatically. You can do it anywhere but I suspect this would be the most appropriate place to do it.
public function register()
{
$this->package('vendor/pkgname');
require __DIR__.'/../routes.php';
}
Upvotes: 3