Quickstart: Laravel 4

Your mileage may vary with this particular quickstart depending on your host. I am using hostmonster, a fairly poor choice for current web development trends, but we aren’t exactly creating the next InstaBookSpaceGramChat here.

1. Download Composer

In SSH, navigate to  the root of your project folder and run the following to download Composer.

curl -sS https://getcomposer.org/installer | php

2. Create your Laravel project

Before you go all copy-pasta crazy down below, make sure to replace your-project-name with whatever it is you are making.

By default, the command to install Laravel project is:

composer create-project laravel/laravel your-project-name --prefer-dist

For my particular environment, I needed to add php and change composer to composer.phar. Again, this is more than likely due to my shared server environment. The updated command looked something like this:

php composer.phar create-project laravel/laravel your-project-name --prefer-dist

Update 09/10/14: For Dreamhost servers, I ended up installing Composer in ~/bin so my Composer command ended up being php ~/bin/composer.phar. I solved this mess by creating an alias via alias composer='php ~/bin/composer.phar'. This allowed me to run composer all over the place.

3. Make /public directory work properly

At this point, your Laravel app is ready to go. Unfortunately, by default, everything needs to be accessed via /public. So if http://www.instabookspacegramchat.com is pointing to your new Laravel project directory, you would need to go to http://www.instabookspacegramchat.com/public to see your new project at work.

This, to me, is a horrible flaw with the base Laravel install. To get around it you can either a) set your document root to /public for your domain or b) create an .htaccess file in your project root.

For option b, I used the following in my newly created .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

4. Create a view and route

If you open up your /app/routes.php file, you’ll see that your default route is being handled by the hello view. The hello view lives in /app/views as hello.php.

To create a view and route to call your very own, you’ll need to go back to your /app/routes.php file and add the following:

Route::get('/foo', function()
{
return View::make('foo');
});

The parameter of View::make() refers to the view. In our case, foo refers to /app/views/foo.php

Keep in mind this is one of the more basic methods of creating a route.  In your later Laravel travels, your route-fu will be much stronger.

Next we need to make our foo view. Save & close your routes.php and create /app/views/foo.php. Throw whatever HTML you want to in this new view and hop on over to http://[url]/foo.

5. Next Steps

  • Creating Blade templates
  • Advanced routing
  • Creating controllers
  • Creating models

Updating/Adding packages via Composer

So you found a fantastic new bundle* package and want to add it to your Laravel 4 installation. Here’s what steps I just took to get it done.

* Bundles were used prior to Laravel 4. If you are looking at a bundle, it isn’t compatible with Laravel 4, by default.

The fantastic package that I just had to have was Imagine, an image processing package.

Depending on your server configuration you may or may not need to type php before any of the scripts below.

If it’s been a while since you’ve added a package to your site, it would be a good idea to make sure you have the latest and greatest Composer.

php composer.phar self-update

Navigate to your project’s laravel directory. It should be a directory with mostly just files related to composer. Files such as composer.phar, composer.json, etc. Once there, use your SSH text editor of choice to edit composer.json. For me, it was this

vi composer.json

Per the instructions at Imagine’s github, you need to update your composer.json as follows:

{
  "require": {
    "orchestra/imagine": "2.3.*@dev"
  }
}

In my case, I already had something in my composer.json so it ended up looking like this:

{
  "require": {
    "monolog/monolog": "1.2.*",
    "orchestra/imagine": "2.3.*@dev"
  }
}

Once that is saved, you can update your Laravel installation via this:

php composer.phar update

For further reading, feel free to check out Composer in more detail at https://getcomposer.org/doc/01-basic-usage.md