What Is New in Laravel 4.2
| Category | Key Changes |
|---|---|
| New Features | Implicit Route Model Binding, '--queued' Artisan Flag, Laravel Forge Integration |
| Improvements | Database Read/Write Connections, Better Encryption, Improved Database Seeding |
| Security | New Encryption Cipher (Rijndael-256), Secure Session Cookies |
| Deprecated | PHP 5.3 Support, mcrypt Extension |
How does Laravel 4.2 improve database handling?
Laravel 4.2 introduces read and write connections for databases. This allows you to define separate connections for read and write operations, which is a huge step towards easier database replication and load balancing setup.
In practice, you configure this in your config/database.php file. You specify a single write host and an array of read hosts. The query builder and Eloquent will automatically use the correct connection.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
What are the new routing capabilities in 4.2?
Implicit Route Model Binding is the standout routing feature. You can now type-hint a model in your route closure or controller method, and Laravel will automatically inject the model instance that matches the corresponding segment in the URI.
This cleans up your code significantly. You no longer need to manually write User::findOrFail($id) in every controller. Just type-hint the User model, and the framework handles the rest.
// Route definition
Route::get('api/users/{user}', function(User $user) {
return $user;
});
// This will automatically inject the User model with the ID from the URL.
How did encryption and security get upgraded?
The encryption system was upgraded from the mcrypt library to use OpenSSL with the Rijndael-256 cipher. This change was made because OpenSSL is more actively maintained and is often more performant than mcrypt.
This matters because it future-proofs the framework's security. All encrypted data will be re-encrypted when you upgrade, so your application remains secure. Session cookies are also now signed with HMAC to prevent modification.
What Artisan command improvements were made?
A new --queued flag was added to the handler:command Artisan command. This flag tells Laravel to generate a command handler that implements the Illuminate\Contracts\Queue\ShouldBeQueued interface.
This is a quality-of-life improvement for developers using queues. It saves you from manually adding the interface to your handler classes, making it faster to dispatch jobs to a queue.
php artisan handler:command EmailPurchaseConfirmation --queued
What are the key dependency and requirement changes?
Laravel 4.2 dropped support for PHP 5.3, requiring PHP 5.4 or greater. This shift allowed the framework to utilize newer PHP features like traits.
It also began the move away from the mcrypt extension in favor of OpenSSL. If you're upgrading, you must ensure your server runs PHP 5.4+ and has the OpenSSL extension enabled.
FAQ
Do I have to change anything for the new encryption?
Yes. The default cipher changed. Laravel will automatically re-encrypt all existing encrypted data, but you must update your cipher setting in app/config/app.php to 'cipher' => MCRYPT_RIJNDAEL_256 to maintain compatibility during the upgrade.
How does implicit model binding work with custom keys?
By default, it uses the id column. If you need to retrieve a model by a different column, you can override the getRouteKeyName method on your Eloquent model to return the name of the desired column.
Is the read/write connection automatic for all queries?
Yes. The Eloquent ORM and query builder automatically perform SELECT statements on the read connection and INSERT, UPDATE, DELETE statements on the write connection. You don't need to manually specify which connection to use for these basic operations.
Why was the --queued flag added to Artisan?
It simplifies the process of creating queueable jobs. Before this, you had to manually implement the ShouldBeQueued interface on your command handlers. This flag automates that step, streamlining development.
Can I still use mcrypt if I prefer it?
No. The framework core was updated to use the OpenSSL methods. The mcrypt extension is deprecated and will be removed in a future version, so you should ensure your server environment supports OpenSSL.