Laravel 4 First Thoughts

The Laravel 4 beta has been out for a few weeks now and it’s beta release just so happened to coincide with me looking for a new PHP framework. I’ve been having a play with it and I must say I’m really impressed. So this is what I’ve discovered so far.

Dependency and package management

Laravel 4 uses a whole load of third party components (in particular from Symfony) in it’s core. All of these dependencies are managed with composer and packagist as are the various separate components of the core of Laravel 4. This means your application logic and the core framework are completely separate and can be easily maintained separately. You do not need to commit an entire framework to version control and then suffer a headache when trying to upgrade it. You just manage your application files in version control and let composer manage and update the core.

This also means including any third party code in your application is super simple. As is creating your own packages.

Class dependency management and service providers

The IoC or Inversion of Control container in Laravel 4 is a really neat piece of code that makes use of PHP 5.3′s closures to make managing object depencies really easy. Service providers are ways of registering your own packages within the system. There isn’t much documentation around at the moment about the service providers in Laravel 4 and it took me a while to figure it out but once you do, you see the power they mask. They give you a single entry point for your packages and allow you to fully define dependencies for particular objects, configuration settings, routes and pretty much anything you want. For example:

class CustomServiceProvider extends ServiceProvider {
	public function register()
	{
		// register a class - this is the IoC container in action, 
		// we could provide all sorts of dependencies or logic to instantiate 
		// new object and whenever we create a new instance of the object it 
		// will be created with all it's dependencies
		$this->app['class'] = $this->app->instance(function() {
			$dependency = new Dependency();
			return new Class($dependency);
		});

		// this class should behave as a singleton
		$this->app['singleton'] = $this->app->share(function() {
			return new Singleton;
		});

		// incldue some custom routes
		include(__DIR__.'/../routes.php');

	}
}

Namespacing

Yes!! Laravel 4 is fully namespaced and so should you be!

Built in database migrations

Laravel 4 comes with built in database migrations. I’m really pleased to see this as database migrations are something I’ve been using for a few years now and there is no way I would work without them!

Summary

There is so much more it does in the way it handles routing, templating and models that I can’t talk about now. These few points are just the first few things that have made Laravel 4 stand out for me.

The documentation is still lacking but that’s to be expected as Laravel 4 is still in beta. However after playing with it for a few days I’m convinced it’s one of the best PHP frameworks available. It is well thought out and fully extendable, and the fact it uses composer is really important to me and made a big impact on what I think of it. It will make collaboration and developing large applications so much less painful and in my opinion this is exactly what a framework is all about.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>