Today we will look at starting to create Laravel 4 Packages with routing options.

In conventional Package development in the past, you would have to create Bundle routes or ask your users to register API controllers, etc. Now with the flexibility of Route::group we can easily create a completely configurable routing package for your Package to run from.

To start, make sure that you have set your configuration in workbench.php for your name and email. Next, run php artisan workbench vendor/package (of course replacing 'vendor' and 'package' with your preference). In this case, I will be starting a FAQ package, so I will go ahead and run php artisan workbench rtablada/laravel-faq --resources (I added resources because I will be including some style and JS in the package as well). This command will have created your Package directory within the workbench folder as noted in the documentation

Now we will take a chart like I have below and turn it into a routes file.

Route Whiteboard

To create the routes file, we first have to do a few things within the service provider, in my case src/Rtablada/LaravelFaq/LaravelFaqServiceProvider.php. Within our boot function we will add require __DIR__ .'routes.php'.

Now we will create our src/Rtablada/LaravelFaq/routes.php file. Traditionally, Packages would come hard-coded with functionality so the routes file might look like this:

Route::group(array('prefix' => 'faq'), function()
{
    Route::get('install', array('uses' => 'Rtablada/LaravelFaq/Installer@create));
    // More routes
});

But, then this creates a delimma for your end users who may already have existing functionality that conflicts with this routing. And, by the way that Laravel loads Service Providers, your routes win out over the end user. Let's fix this by adding functionality which I found helpful when working on the WardrobeCMS refactor.

To start, we will create a src/config/config.php file (if you have a lot of complex configuration, you could easily move this to a src/config/routes.php file):

<?php return array(
    'routing' => array(
        'prefix' => 'faq',
        // 'subdomain' => 'faq.site.com',
    ),
);

This is pretty standard configuration syntax if you are used to using Laravel. As you can see, I have a commented out section for subdomain with a simple default. By convention, I assume that the client will want this to be prefixed, but with a simple switch of comments, they will be able to move it to a subdomain quickly without having to know much about Laravel routing.

Now, comes the question of how to access this configuration within our routes. Luckily, this is extremely simple and can be accomplished by a quick swap within the old src/Rtablada/LaravelFaq/routes.php from above:

Route::group(Config::get('laravel-faq::routing'), function()
{
    Route::get('install', array('uses' => 'Rtablada/LaravelFaq/Installer@create));
    // More routes
});

That is all! With just a simple configuration file and one small change in your routes file, you have created a powerful customizable package for end-users. Next week, we will review a few more techniques that are going into the creation of this FAQ package. You can also follow the code on my Github repo.