Structure your Laravel App with Namespaces
Laravel 4 is really flexible and allows you to setup your application however you want, after all the majority of Laravel/Illuminate packages can be used in external apps and other frameworks. See my blog on Composer and Codeigniter: Part I Replace The Database/Model Layer for information on how to integrate Laravel/Illuminate packages into Codeigniter. In this blog I’ll be showing how with PHP namespaces we can move the core parts of an app and put them wherever we want and use any structure we choose.
To get started we’ll grab a fresh copy of Laravel 4 and set it up, as this is discussed in great detail on multiple websites, I’ll link you to some places that have explained this. The official details on how to install Laravel 4 can be found here and a very good article walking through how to setup Laravel 4 and updating can be found here.
Once setup we need to edit the composer.json file to map our namespace in the Composer Autoloader, so open up composer.json and add the following in autoload under psr-0 like:
"autoload": { "psr-0": { "FooExample": "src/" } }
The “src/” part is where the location of where these Namespaced classes will be, in this example we’ll be putting it under src which sits in the same directory as the composer.json file. However this can be anywhere and you just need to adjust the path accordingly. You can replaceFooExample with whatever you’re namespace will be, you just need to create a directory under whatever path you passed for your namespace. I.e. We need to create a FooExample directory under src as our namespace is FooExample and we’ve set the location as src.
Then run the composer update command, this will update the Composer Autoloader, now you should be good to start using namespaces. In the next part I’ll discuss how to map Routes to Namespaced Controller Classes and how to make Database calls with Namespaced Model Classes.
Creating your Namespaced Controller Class
To create your namespaced Controllers just drop into the directory mapped to your Namespaced Classes, for example in this post we created it in src/FooExample. So in this folder we need to create another directory called Controller, then in here let’s create a Controller Class caled Demo, you could call it DemoController but I think it’s uneeded when using namespaces.
In Demo.php or whatever you decided to call it, let’s enter the following code:
<?php // Replace FooExample with your namespace namespace FooExample\Controller; // We can import reference Classes and Functions that are not in // this Namespace with 'use' or alternatively we can tell the // compiler that they are not in this namespace with the fullpath // or by using them like: // \Controller or return \View::make('hello'); use Controller; use View; class Demo extends Controller { public function showIndex() { return View::make('hello'); } }
Mapping Routes to Namespaced Controllers
Now we have our Namespaced Controller we need to map this so the Router knows what to do when an endpoint is accessed. Therefore to map routes to Namespaced Controllers you just need to reference your Class(es) in routes.php as:
// Map Individual Route Route::get('/', array( 'uses' => 'FooExample\Controller\Demo@showIndex' )); // Or if your're using Resourceful Controllers then Route::resource('demo', 'FooExample\Controller\Demo');
Creating your Namespaced Models
Now create another folder called Model in your namespaced directory, this is the same place where you created the Controller directory. I have put the bare minimum Illuminate/Laravel 4 Model below, feel free to update it as you wish.
<?php // Replace FooExample with your namespace namespace FooExample\Model; class Demo extends Model { protected $table = 'demoTable'; // Put your table name }
Accessing Namespaced Models
To access Namespaced Models your Controllers, Libraries etc call them as you normally would with the slight difference of adding your namespace before it, for example the above Model would be called as:
new FooExample\Model\Demo();
Add Namespaced Custom Validators
Guide on how to add Custom Validatons Rules using Namespaced Classes.
Conclusion…
As you can see using Namespaced Classes with Laravel/Illuminate is very easy, if there is anything specific you would like to see Namespaced or would like more details please let me know in the comments section.