Laravel 5 is already out, but the fear of change is taking everyone. We keep hearing people complaining about some radical changes. Like, why this new folder structure? Will my application break if I do a composer update
?
In this article, we’re going to look at how to migrate your existing Laravel 4 application to Laravel 5 and understand the new folder structure.
Key Takeaways
- Upgrading from Laravel 4 to Laravel 5 involves several steps including updating the composer.json file, updating routes, controllers, and views, and modifying any custom code to use new features and changes in Laravel 5.
- Laravel 5 introduces several new features and improvements such as a new directory structure, improved routing, better handling of environment configuration, and new components like Socialite, Elixir, and Scheduler.
- The process of upgrading to Laravel 5 can be complex and time-consuming, depending on the size of the application. However, it is not necessary to upgrade to the new folder structure; one can keep the old one and only update composer dependencies, although this is not the recommended way of doing it.
Installation
My existing Laravel 4 application is a demo from a previous article about using Google Analytics API. The application doesn’t have too much code, but it will do the job for our tutorial.
Let’s first install Laravel 5 on our computer and create a temporary folder to hold our Laravel 4 version of the application.
composer create-project laravel/laravel --prefer-dist
I prefer to install Laravel via composer, but you can visit the documentation to read more about the Laravel installer.
You can use the Vagrant box from the repository, or use Homestead Improved. If everything goes well, you should see the Laravel 5 welcome page.
Configuration Files
The old app/config
folder now lives under the root of your application, thus we have to move our app/config/analytics.php
to config/analytics.php
. The credentials are pasted directly to the file, so why not use environment variables.
// config/analytics.php
return [
'app_name' => env('app_name'),
'client_id' => env('client_id'),
'client_secret' => env('client_secret'),
'api_key' => env('api_key')
];
// .env
app_name='YOUR APP NAME'
client_id='YOUR CLIENT ID'
client_secret='CLIENT SECRET'
api_key='API KEY'
The .env
file is automatically loaded and can be used to separate your local environment configuration from production, tests, etc.
Routing
Laravel 4 routes are registered inside app/routes.php
. In Laravel 5, everything that is HTTP related is grouped under the app/Http
folder, including routes, so let’s move our app/routes.php
to app/Http/routes.php
.
Filters
Laravel 5 has moved from filters to middlewares, so if you have any filters inside your routes, make sure you change to middlewares.
Route::get(‘/report’, [‘middleware’ => ‘auth’, function()
{
//
}]);
If you have a custom filter, you can migrate it to be a middleware. I have a GoogleLogin
middleware that I’m using in my routes, the implementation will be like the following.
// app/Http/Middleware/GoogleLogin.php
class GoogleLogin
{
public function handle($request, Closure $next)
{
$ga = \App::make('\App\Services\GoogleLogin');
if (!$ga->isLoggedIn()) {
return redirect('login');
}
return $next($request);
}
}
// app/Http/Kernel.php
protected $routeMiddleware = [
'google_login' => 'App\Http\Middleware\GoogleLogin',
];
// app/Http/routes.php
Route::any('/search', ['middleware' => 'google_login', 'as' => 'search', 'uses' => 'GoogleController@search']);
The CRSF protection middleware is added by default. If you want to remove it, you can go to the app/Http/Kernel.php
file and comment out the proper line.
Controllers
Because our controllers are considered part of the Http logic, we need to move our app/controllers/*
to app/Http/Controllers
and namespace them with App\Http\Controllers
. One last problem you need to fix is to change the BaseController
to the Controller
class.
If you don’t like the App
root namespace, you can change it globally using the following artisan command.
php artisan app:name MyApp
Migrations
Our Google Analytics application doesn’t have any local database interactions, but the upgrade process is worth mentioning.
The app/database
directory now lives inside the /database
folder, and you only need to move your file there. The directory already contains a users
and a password_resets
table that you may want to delete or update depending on your needs.
Models
The models folder from Laravel 4 is gone, and Laravel 5 puts the User
model directly inside the app
folder as an example. You can copy your models in there too, and namespace them with App
.
However, if you don’t like the idea of having your models there, you can create a new folder under the app
directory called Models
, but don’t forget to namespace your classes with the App\Models
namespace.
namespace App\Models;
class User extends Eloquent {
…
}
Application Services
Our src
folder contains a GA_Service
and a GA_Utils
class. If we consider them to be services, we can put them inside app/Services
. Otherwise, we can create a new folder called app/GA
where we’re going to store our service classes. This will cause a problem since we didn’t use PSR-4 autoloading at first, so we need to update our class references inside the controller with the proper new namespace.
Views
The application views are moved from the app/views
folder to the resources/views
folder.
The resources
folder also contain the lang
folder for your application’s localization, and an assets
folder for your front-end assets. Laravel 5 introduced Elixir which adapts the Gulp task runner to the Laravel development environment.
Composer
Make sure you copy your application’s composer dependencies and do any necessary upgrades. For our demo, I’m going to move my "google/apiclient": "1.1.*"
to the new composer.json
and do a composer update
to reflect the changes.
Forms and HTML
The illuminate/html
package is removed from the default installation on Laravel 5 and you will need to install it separately.
To bring back the HTML helpers to your project, you need to add the "illuminate/html": "5.0.*"
package to your composer.json
and run composer update
, then you need to add the 'Illuminate\Html\HtmlServiceProvider'
to your config/app.php
providers array. If you would like to use the Html
and Form
facades inside your blade templates, you can append the following facades to your config/app.php
facades array.
// config/app.php
'aliases' => [
'Form'=> 'Illuminate\Html\FormFacade',
'HTML'=> 'Illuminate\Html\HtmlFacade',
],
Conclusion
The complexity and duration of the process of upgrading to Laravel 5 always depends on your application’s size, and maybe the process will be much longer than this example for your particular case. In this article, we tried to explain the common process which should take care of most if not all things that need changing.
You are not forced to upgrade to the new folder structure, and you can keep the old one and only update your composer dependencies, but this is not the recommended way of doing it. If you have any questions or comments, make sure to post them below. For more information, see the full version upgrade guide.
Frequently Asked Questions (FAQs) about Laravel 4 to Laravel 5 Upgrade Guide
What are the major differences between Laravel 4 and Laravel 5?
Laravel 5 introduced several new features and improvements over Laravel 4. These include a new directory structure, improved routing, better handling of environment configuration, and the introduction of new components like Socialite, Elixir, and Scheduler. Laravel 5 also introduced a new command-line interface called Artisan, which provides a number of helpful commands for common tasks.
How do I handle environment configuration in Laravel 5?
Laravel 5 introduced a new way of handling environment configuration. Instead of using a single .env.php
file, Laravel 5 uses a .env
file for each environment. This makes it easier to manage different configurations for different environments. You can set your environment variables in the .env
file and Laravel will automatically load them.
What is the new directory structure in Laravel 5?
Laravel 5 introduced a new directory structure that is designed to be more intuitive and flexible. The app
directory is now the root of your application, and it contains several subdirectories for different parts of your application, such as Http
, Providers
, and Console
. The public
directory is now the web server root, and it contains your assets, such as images, JavaScript, and CSS files.
How do I upgrade from Laravel 4 to Laravel 5?
Upgrading from Laravel 4 to Laravel 5 involves several steps. First, you need to update your composer.json
file to require the latest version of Laravel. Then, you need to update your application’s code to use the new features and changes in Laravel 5. This may involve updating your routes, controllers, and views, as well as any custom code that you have written.
What is Laravel Elixir and how do I use it?
Laravel Elixir is a new component in Laravel 5 that provides a clean, fluent API for defining basic Gulp tasks. It supports common CSS and JavaScript pre-processors like Sass and CoffeeScript, and it also provides a convenient method for versioning and concatenating your assets.
How do I use the new routing system in Laravel 5?
Laravel 5 introduced a new routing system that is more flexible and powerful than the one in Laravel 4. Routes are now defined in the app/Http/routes.php
file, and you can group routes, apply middleware to them, and even namespace them.
What is Laravel Socialite and how do I use it?
Laravel Socialite is a new component in Laravel 5 that provides a simple, convenient way to authenticate with OAuth providers. It supports several popular providers out of the box, and you can also add your own custom providers.
How do I use the new Artisan commands in Laravel 5?
Laravel 5 introduced a new command-line interface called Artisan, which provides a number of helpful commands for common tasks. You can use Artisan to generate boilerplate code, run database migrations, and even start a local development server.
What are the new features in Laravel 5.0?
Laravel 5.0 introduced several new features, including a new directory structure, improved routing, better handling of environment configuration, and the introduction of new components like Socialite, Elixir, and Scheduler. It also introduced a new command-line interface called Artisan.
How do I handle database migrations in Laravel 5?
Laravel 5 provides a powerful database migration system that allows you to version control your database schema. You can create migrations using the Artisan command-line tool, and then run them using the migrate
command. This makes it easy to apply changes to your database schema across different environments.
Younes is a freelance web developer, technical writer and a blogger from Morocco. He's worked with JAVA, J2EE, JavaScript, etc., but his language of choice is PHP. You can learn more about him on his website.