HTML5 Zone is brought to you in partnership with:

Andy is a freelance journalist based in Coventry, United Kingdom. He also codes web applications. Andy is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Started WIth Laravel PHP

05.12.2013
| 5906 views |
  • submit to reddit

If you code for the web, you’ll most likely have seen all the fuss about the Laravel PHP framework. I’ve decided to take a look and see what all the fuss is about.

Like many developers, I’ve spent quite a bit of time developing with CodeIgniter. I’ve also just authored my first coding book entitled “Jump Start Rails” which covers Rails 4.0.

I only mention that because, while I’ve never used Laravel before, I do know what I’m looking for in a web framework. I won’t be spending too much time on the actual construction. This will be a practical series of posts.

Installing Laravel

One of the things I like about CodeIgniter is the fact that you just drop it into a folder your web server knows about, and away you go. The only other configuration is setting your database credentials and some basic information about your site (url, whether you intend to use mod_rewrite), and what resources to autoload.

The potential downside of that simplicity is, all the framework files end up in a publicly accessible folder. You can change that, but it requires a couple of extra steps. That doesn’t happen with Ruby on Rails – it has a public folder as soon as you create the app. Laravel? Yep, it has a public folder too. So when you create you local domain, you have to make sure and point it to the public folder:

Laravel

There are some permissions to set too. You need to give write permissions to /storage/views. Aside from that, there is very little else to do at this stage. We are close to being ready to fire up the app in our browsers.

So far, we’ve downloaded the code, dumped it into a web enabled folder, created a local domain, set permissions on one folder, and we are good to go. That sounds like minimum fuss doesn’t it?

There are a couple more things to do though. First, open config/application.php and alter these options:

'index' => '',
 
/*
|--------------------------------------------------------------------------
| Application Key
|--------------------------------------------------------------------------
|
| This key is used by the encryption and cookie classes to generate secure
| encrypted strings and hashes. It is extremely important that this key
| remains secret and it should not be shared with anyone. Make it about 32
| characters of random gibberish.
|
*/
 
'key' => 'YourSecretKeyGoesHere!',

You do need to put something in the key, and you can remove the ‘index.php’ so that we can use pretty urls. Now you can fire up your browser and point it to your local domain:

Laravel up and running

Good, we are up and running. One more thing we can do though, is add a database. And since we are creating a blog, it might be worth adding support for sessions too, so we can enable logins. The default database is MySQL, but you can pick from several other too.

Open application/config/database.php and add your database credentials in the MySQL section:

'mysql' => array(
    'driver'   => 'mysql',
    'host'     => '127.0.0.1',
    'database' => 'database',
    'username' => 'root',
    'password' => '',
    'charset'  => 'utf8',
    'prefix'   => '',
),

Working With Sessions

Session data is stored in a database table so we’ll need to add that. Also, Laravel uses migrations, which are a kind of version control for your database. If you’ve worked with Rails, you’ll appreciate how useful migrations are.

Laravel comes with a handy CLI tool called Artisan. We need to use it to enable our database to keep track of our migrations. So open up your Terminal app, change into your project directory and do:

php artisan migrate:install

You’ll see a response like this:

Migration table created successfully.

That will be your migrations table created. Next, back in Terminal do:

php artisan session:table

Which will produce a result something like:

An application key already exists!
Great! New migration created!
Migrated: application/2013_05_08_213358_create_session_table

And that will be your sessions table created.

Finally…

We are now poised to begin digging deeper into Laravel and get a feel for how it really works. Setting up a new app is easy, and the CLI tool is very useful. So far, so good.



Published at DZone with permission of Andy Hawthorne, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)