Learninglaravel Sample
Learninglaravel Sample
Jack Vo
This book is for sale at http://leanpub.com/learninglaravel
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Introducing Laravel 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Say “Hi” to Laravel 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Laravel History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Laravel is a MVC Framework? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Places to Learn Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
A Special Thanks
Thank you for purchasing this book.
If you want to help me finding typos and other issues, or want to give some feedback, feel free to
contact me at:
www.twitter.com/LearningLaravel¹
or
www.facebook.com/LearningLaravel²
or
www.LearningLaravel.net³
Here’s a list of people who have helped me to bring this book to you:
¹http://www.twitter.com/LearningLaravel
²http://www.facebook.com/LearningLaravel
³http://www.LearningLaravel.net
Welcome to Learning Laravel 3
Translations
If you’re interested in translating this book into a different language, please contact me at:
[email protected]⁴
I will offer a 50/50 split of the the profits from the translated copy.
We dive into building some simple web applications right away. It’s the best way to learn Laravel.
While some other programming books teach you the basic things first, I know all of us love to do
something while we’re learning.
- Chapter 2 - Building A To-do List Application
We have a fully responsive home page from Chapter 1. We will use it as a template for our To-do list
application. In this chapter, we learn more about Laravel special features, such as: Blade Template,
Schema Builder, Eloquent ORM, Controllers, Composer and Artisan.
PART 3 - AN ALTERNATIVE LARAVEL DOCUMENTATION
If you’re a Laravel programmer, you can read this section as a documentation and learn more about
it. If you’re a beginner, don’t worry, you will be a Laravel programmer soon. There are many design
philosophies and principles in this section as well.
PART 4: LARAVEL CHEAT SHEET
If you want a cheat sheet for Laravel. Here it is.
PART 5: BUILDING A COMPLETE CMS FROM SCRATCH
- Chapter 2 - Building A Responsive Website From Scatch
Here we’ll build a dynamic website using our knowledges from the book. In the end, our application
looks like this:
Welcome to Learning Laravel 5
This is one of my themes on Themeforest. If you love the theme, you can buy it at the link below
right away. If you worry about the price, you can also send me a message, I will give you a big
discount:
Style Vintage Drupal Theme⁵
APPENDICES
- Basic HTML5, CSS3, Twitter BootStrap and PHP knowledge
If you don’t know PHP, you don’t even know how to code, this section is for you! You can skip this
part if you like as well because it’s for absolute beginners.
Revision History
Laravel History
In 2011, Taylor Otwell, a great web developer has created an open source PHP framework, he called
it Laravel. For only just 2 years, many developers around the world have been developing and using
Laravel to build their applications. Laravel has come to version 4.1 today. It has many features such
as built in web applications, session management, database management, Composer and many many
more.
Laravel is a full stack framework, it means that you can develop web application completely from
scratch using its amazing database wrapper called Eloquent ORM and its own templating engine
called Blade. Many problems in the process of creating web application have been solved by Laravel.
Laravel is a great tool, a great time saver to help you build things faster and faster. There are many
reasons for using Laravel to develop web applications. One of the reasons is, Laravel has a welcoming
and supportive community. Unlike Symfony or Zend framework, you can find many code snippets,
tutorials, courses about Laravel, especially version 4. Even though the Laravel 4 has just been released
a few months ago.
Laravel is not only Taylor Otwell’s product. It’s the product of a big community. It’s an open source
framework, thus hundred developers worldwide have been providing many new features, bug fixes,
ideas. You can easily ask questions in the forum, or through Laravel IRC channel. Want to join?
Register here:
Introducing Laravel 4 8
This part will be an alternative documentation. You will learn how to install Laravel on different
systems, read this part as a cheat sheet, and know everything about Laravel framework here.
A Guide to Install Laravel 4
I will show you how to install Laravel 4 on Mac and Windows from scratch. It means that you will
know how to install PHP, Mysql, Mcrypt, Composer and everything to get your app running!
Please note that there are many ways to install PHP, Composer, Mcrypt, etc. I will show you the
basic ones first, and then I’ll update other methods later. If you love to do it in a different ways, go
ahead and do it! You will learn a lot by doing it yourself.
What is Terminal?
Terminal is terminal emulator provides an environment for Unix shells, which allows the
user to interact with the operating system through the command line interface. On Mac,
you can find it in Applications -> Utilities -> Terminal.
Open Terminal and type in the below code to know which PHP version you’re using:
1 php -v
It means that you currently have PHP 5.4.17 on your machine. If you have PHP version 5.4 or newer,
then skip this section.
Using OS X Mavericks?
Mac OS X 10.9 ships with PHP 5.4.17 out of the box. So you can skip this part if you’re
using Max OS X 10.9. However, just run php -v to double check that you’re using a correct
PHP version.
If you see that you have an older PHP version or you don’t have PHP, then let’s install it. Type this
line or copy and paste in into the Terminal:
Wait for while, type y and hit Enter if it asks you, then you paste the following line:
1 export PATH=/usr/local/php5/bin:$PATH
Done! if we check using php -v again, it will show that you have PHP 5.4.xx.
3- Installing Mcrypt PHP Extension
We’re going to install Mcrypt PHP Extension now.
What is Mcrypt?
Mcrypt is a file encryption method using secure techniques to exchange data.
Open Terminal, change directory (cd) to the home account and make a directory that you will work
in, call it mcrypt. You can do these things by typing into Terminal (line by line):
1 cd ~
2 mkdir mcrypt
3 cd mcrypt
This will make a folder called mcrypt in my home account folder, which is Users/∼YourUsername.
For example, I can find the mycrypt folder on my Mac at: Mac/Users/JV.
Get libmcrypt 2.5.8 from Sourceforge:
A Guide to Install Laravel 4 14
1 php -v
Move both of these files that you downloaded into your working directory (mcrypt) and go back to
Terminal:
1 cd ~/mcrypt
Expand both files via the command line (or just double click them in the Finder and skip this part):
1 rm *.gz
1 cd libmcrypt-2.5.8
1 ./configure
2 make
3 sudo make install
When you type sudo, usually it will ask for a password, enter your system password and move on.
You now have libmcrypt configured and libraries now installed, it’s time to make the mcrypt
extension. Enter:
²²http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
²³http://php.net/releases/index.php
A Guide to Install Laravel 4 15
1 cd ../php-5.4.17/ext/mcrypt/
2 /usr/bin/phpize
1 Configuring for:
2 PHP Api Version: 20100412
3 Zend Module Api No: 20100525
4 Zend Extension Api No: 220100525
Autoconf Errors
If you see “Cannot find autoconf. Please check your autoconf installation…” error occurs
after you try the following compile of mcrypt, then autoconf is not installed. Install it using
the following guide, otherwise you can skip it.
Go to Terminal, type:
1 cd ~/mcrypt
2 curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
3 tar xvfz autoconf-latest.tar.gz
4 cd autoconf-2.69/
5 ./configure
6 make
7 sudo make install
After installing Autoconf, you need to go back to the folder mcrypt/php-5.4.17/ext/mcrypt, enter:
1 cd ~/mcrypt/php-5.4.17/ext/mcrypt
2 /usr/bin/phpize
You should see the output like this, without “Cannot find autoconf…” error:
1 Configuring for:
2 PHP Api Version: 20100412
3 Zend Module Api No: 20100525
4 Zend Extension Api No: 220100525
Good, make sure you’re still in the mcrypt folder, now enter:
A Guide to Install Laravel 4 16
1 ./configure
2 make
3 sudo make install
The output
Last step, You need to enable mcrypt.so PHP extension. Open /etc/php.ini and add the line below at
the end of the file:
1 extension=mcrypt.so
If there is no php.ini file, then you need to make one from php.ini.default in the same location using
Terminal:
or
1 sudo vi /etc/php.ini
Make sure that you have the line below at the end of your php.ini:
1 extension=mcrypt.so
If you see error: The detect_unicode setting must be disabled. Use this command instead:
1 php composer.phar
Final step, execute this command to easily access Composer everywhere on your system:
1 cd desktop
Cool, now you’re at your desktop, you just need execute one command in the command line to
create a Laravel application,replacing learningLaravel with the name of your project:
After executing this command, Composer will download all components of Laravel, and put them
together into your application folder called learningLaravel. You will see something like this when
it finishes:
A Guide to Install Laravel 4 19
1 cd learningLaravel
Open your web browser, and go to http://localhost:8000, enjoy your first site!
2- Enable OpenSSL
Go over to php.ini (located in C:\xampp\php) and open it with a text editor. Find:
1 ;extension=php_openssl.dll
And remove the “;”, then save the file. So it should look like this:
²⁴http://www.apachefriends.org/en/xampp-windows.html
²⁵http://www.apachefriends.org/en/xampp-windows.html#522
A Guide to Install Laravel 4 21
1 extension=php_openssl.dll
1 # Virtual hosts
2 Includes "conf/extra/httpd-vhosts.conf"
1 #Includes "conf/extra/httpd-vhosts.conf"
²⁶http://getcomposer.org/doc/00-intro.md#installation-windows
²⁷http://code.google.com/p/msysgit/downloads/list?q=label:Featured
A Guide to Install Laravel 4 22
1 cd C:/xampp/htdocs
1 ls
You should see your folder there in the output, like this:
1 FF learningLaravel
1 cd learningLaravel
Composer will be going to download and install Laravel into that folder. After it’s done, you will
see something like this:
Good job, you have just installed Laravel. You can open your web browser and go to your site at
this address:
http://localhost/learningLaravel/public²⁸
If there are some errors, don’t worry, we will fix it soon.
1 <VirtualHost *80>
2 DocumentRoot "C:/xampp/htdocs/learningLaravel/public"
3 ServerName learningLaravel.dev
4 </VirtualHost>
What we’re doing here is putting the path to our application folder (learningLaravel) into
DocumentRoot. And set the ServerName to learningLaravel.dev. So we just only need to type
learninglaravel.dev into our web browser to access it!
Now you should restart the Apache using XAMPP Control Panel. Opening up the Xampp control
panel, clicking ‘Stop’ (next to ‘Apache’), waiting for it to stop, then clicking ‘Start’.
7- Last step! Edit the hosts file
Go to C:/Windows/System32/Drivers/etc and edit the hosts file. The hosts file require Adminis-
trator permission. So you need to open your notepad as Administrator (Ctrl -> Right Click -> Open
As Administrator), and then open the hosts file as Administrator.
Ok, put “127.0.0.1 learninglaravel.dev” into the hosts file, below this line:
1 127.0.0.1 localhost
1 127.0.0.1 localhost
2 127.0.0.1 learninglaravel.dev
Save the file and… Congrats! You can now access your site using this link:
http://learninglaravel.dev²⁹
You should see this screen:
²⁹http://learninglaravel.dev
A Guide to Install Laravel 4 24
8- Extra step
If you don’t see the screen above, instead it shows many project files like normal localhost does.
Follow these steps below to fix it:
First, you need to make sure that rewrite_module and vhost_alias_module modules are enabled.
The following lines should be uncommented in xampp/apache/conf/httpd.conf and in xampp/a-
pache/conf/original/httpd.conf, make sure to remove the # sign:
1 <VirtualHost *:80>
2 DocumentRoot "c:/xampp/htdocs/laravel/public"
3 ServerName learningLaravel.dev
4 ServerAlias www.learningLaravel.dev
5 </VirtualHost>
A little note: just remind that you must have PHP and Composer installed on your system to install
Laravel.
First, you need to download the Laravel installer PHAR archive at the link below:
Download Laravel Installer³⁰
Put it in your working folder, or your desktop. I put it on my desktop. If you’re doing the same, then
navigate to your desktop:
1 cd desktop
Good, you’re in the desktop. Now just type this single line to install Laravel in no time:
As you see, I use mv command to move laravel.phar on my desktop to /usr/local/bin and rename
the file to laravel. Ok, the last trick is, you need to set permissions for the file, then we can use it:
1 chmod +x /usr/local/bin/laravel
Well done! Now, everytime you want to create a new Laravel app. You just navigate to a place (for
example: desktop), and execute this command:
³¹http://leanpub.com/learninglaravel
³²http://learninglaravel.net