Getting Started with Laravel 5
A little over a year ago, I decided that I wanted to learn Laravel. I had seen quite a few tweets about the framework and it piqued my interest.
I spoke with my boss, and he was okay with me learning Laravel in my down time so that I could rewrite our custom CMS package (that we use for larger custom website projects) using it, thus moving away from CodeIgniter.
I managed to convince my boss to buy a book about Laravel (Learning Laravel 4 Application Development). After reading through the book and some tutorials I found online, I felt comfortable enough with Laravel to start rebuilding my custom CMS package, using Laravel 4.2 … which was the current version at the time.
Then … just as I got started … I had to switch gears and work on projects that used WordPress.
Now … a little over a year later … I have some time between client projects and can actually get back to Laravel development. Now that Laravel is at version 5.1. #doh
From what I have read, there are quite a few differences between Laravel 4.2 and Laravel 5. Since I hadn’t gotten very far into the development of my new CMS package, I decided to just start from scratch. All I had really gotten done was create the admin theme anyway. So I’m not throwing away very much code.
While I set up my first test application using Laravel 5.1, I outlined the steps that I followed to get things up and running. I didn’t have a book to use as a reference this time, and I wasn’t able to find the kind of tutorials I was looking for, so I thought I would share my process, which came from the Laravel documentation and numerous other articles scattered across the web. If I help out at least one other person, then I’ve successfully given back to the php community (which I would like to start doing on a regular basis). If not, I can at least refer back to this post myself for the inevitable situation that I get pulled back into a WordPress project and don’t get back to Laravel for several months. 🙂
Assumptions … You have Composer installed. You are using MAMP. You have a VirtualHost set up for the domain that you are using for your application, and you have added a record to your system’s HOSTS file.
1) Install Laravel using the following command. This will create a directory with the <project-name> and download all the needed files for a Laravel application, so make sure you are in the appropriate directory in Terminal before you run the command.
composer create-project laravel/laravel <project-name> --prefer-dist
2) Modify the environment variables in the .env file. If the file doesn’t exist, rename the .env.example file and then make your changes. Add the following variables to the file and set for your application:
APP_URL=http://yourdomain.dev APP_TIMEZONE=America/Chicago DB_CONNECTION=mysql MAIL_SENDER_EMAILfirstname.lastname@example.org MAIL_SENDER_NAME=System Admin
3) Modify the /config/app.php file so that the url and timezone options pull in the values from the .env file. This will allow you to change the values based on the environment the application is being run on without needing to duplicate the app.php file for the various environments.
'url' => env('APP_URL', 'http://localhost'), 'timezone' => env('APP_TIMEZONE', 'UTC'),
4) Modify the /config/mail.php file to replace the Global “From” Address values with the variables set in the .env file.
'from' => ['address' => env('MAIL_SENDER_EMAIL', null), 'name' => env('MAIL_SENDER_NAME', null)],
5) If you are developing using a MAMP server and using a MySQL database (like I am), you will need to modify the /config/database.php file to include the “unix_socket” in the MySQL settings. Add the following to the connections.mysql options to get things to work properly.
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
6) Modify the /config/session.php file as necessary for your application. Some variables to consider …
- driver – where the session data is stored; defaults to file but there is an option of database … this value can actually be set in the .env file.
- expire_on_close – defaults to false but if you want the session to end when the browser is closed, set it to true
- encrypt – if you want the session data to be encrypted, set to true
- connection – if you are using a driver of database, set this to the same database connection you are using in the database.php config file
You should now have a functioning application running on Laravel. To test, load up the URL you set up for the app. You should see the “Laravel 5” welcome banner.
While building your application, controllers should be saved to /app/Http/Controllers/ and views should be saved to /resources/views/.
This process will allow you to create a basic website. There are other Composer packages that can be brought into your project to help you create a more advanced website.
One package that I found when I was figuring things out last year was the teepluss/theme package, which allows you to create themes. The package was updated for Laravel 5, so I’m still using it now. Just how to integrate it … I’ll leave that for another post.