Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package Controllers in Laravel 4

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';
   })
like image 889
Lars Steen Avatar asked Jan 19 '13 16:01

Lars Steen


People also ask

What are controllers in Laravel?

Controllers can group related request handling logic into a single class. For example, a UserController class might handle all incoming requests related to users, including showing, creating, updating, and deleting users. By default, controllers are stored in the app/Http/Controllers directory.

What are packages in Laravel?

What are Laravel packages? Packages in PHP are a collection of routes, controllers, and views that are configured to add or extend the functionality of a Laravel application.

What is Invokable controller Laravel?

Laravel provide a single action controller called invokable controller which contains a invoke method to perform a single task. So for doing only single task we can use this invokable controller.

Where are controllers file located in Laravel?

In Laravel, a controller is in the 'app/Http/Controllers' directory. All the controllers, that are to be created, should be in this directory. We can create a controller using 'make:controller' Artisan command.


1 Answers

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');
like image 108
Kevin Perrine Avatar answered Sep 23 '22 06:09

Kevin Perrine