5
votes

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';
   })
4
Please post your routes.php code showing what you have triedLaurence
Hey did you manage to get this to work ? if so. could you post an answer with the sollution ?DonSeba
Could you be more specific on "can't get the routing to work"? What do you get? 404 error?akhy
@AkhyarAmarullah I just get a blank page, no error.Lars Steen

4 Answers

3
votes

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');
10
votes

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');
2
votes

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';
}
-1
votes

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.