Getting started with Laravel 5.1



Laravel is growing in popularity, and is arguably the most popular php framework to date. Looking at Google Trends, Laravel is the fastest growing framework, and it's easy to see why.

So how can you get started? Simple enter this command into your terminal. Assuming you have composer installed, this will fetch Laravel installer.

$ composer global require "laravel/installer=~1.1" Now you can use this installer to create Laravel projects

$ laravel new TestApp Now change directory into your new project

$ cd TestApp Hopefully you'll already have homestead installed. Homestead is a pre-configured vagrant VM for Laravel projects. This is great because it has everything you need to get started.

So make sure you have homestead installed and running and run $ homestead edit to edit your homestead config.

If you don't have homestead installed then here's how.

Add the vagrant box
$ vagrant box add laravel/homestead.

Clone the Homestead wrapper for the homestead box
$ git clone https://github.com/laravel/homestead.git Homestead

Now run the init script to run the set-up
$ bash init.sh

Now that you have homestead installed, let's edit your homestead config to look something like the following:

folders:  
    - map: ~/Development/Tutorials/TestApp
      to: /home/vagrant/TestApp

sites:  
    - map: test-app.dev
      to: /home/vagrant/TestApp/public
      hhvm: true

databases:  
    - test_app

But change the ~/Development/Tutorials/TestApp to wherever you created your Laravel 5 application. This will tell your vagrant box to look in that directory for the files in order to transfer them into your vagrant box. It will also create your nginx hosts and database for your application.

Notice I've also included the hhvm: true line, which means your application will make use of Facebook's HHVM, this will make your code a little quicker as it caches php code into bytecode instead of re-interpreting the same code over and over.

Now run $ homestead up. This will fire up and create your VM based on your configuration.

Edit your host files and include the following entry:

192.168.10.10 test-app.dev
Or if you're running on OSX and use Gas Mask:

If you open test-app.dev up in your browser, you should not see the Laravel default page.

Pro tip: You'll be using the command $ php artisan a lot. So it's worth adding an alias in your homestead box for that. To do that, ssh into your homestead box ($ homestead ssh) hit $ vim ~/.bashrc (or $ nano ~/.bashrc if you prefer nano). Enter the following in a new line somewhere: alias art="php artisan", save, exit and run $ source ~/.bashrc to reload your terminal variables.

Now instead of having to run $ php artisan you can just run $ art. It may seem trivial, but you'll be using that command a lot when developing with Laravel, so it'll save you time in the long run.

Controllers

To create a controller, ssh into your homestead box $ homestead ssh then cd into your application. In our case cd TestApp then run $ php artisan make:controller HelloController.

This will generate a new controller in app/Http/Controllers. Open that controller and you'll notice Laravel has created some bootstrap code in there for you. This is useful for creating CRUD application, but you might not always need it. So just remove everything apart from your index function. Edit that function to look as followed:

public function index($name)  
{
    return view('hello')->with('name', $name);
}

Now create a view (hello.blade.php) in resources/views. Add some basic html in there such as:

<h1>Hello, {{ $name }}!</h1>  

Now open app/Http/routes.php and add the following line:

Route::get('/hello/{name}', 'HelloController@index');  

If you locate to test-app.dev/hello/your-name you'll see that our route has passed your name as a url parameter, through our 'HelloController' and to our view.

So as you can see, other than some folder structure changes, not a great deal has changed in terms of the basics. There are some great new features which I'll cover in some not so distant future posts.

That's the very basics of getting up and running in Laravel 5.1! Hope this helps :)