Introducing Laravel

Before spending any more time building my own PHP / PDO framework - I thought I should just check to see if there were any new kids on the block.

Thats when I discovered Laravel.

Go See for Yourself

Documentation

Laravel seems to take the best of Ruby on Rails, Groovy and Grails and other PHP frameworks. And simply does it better - or you could say - it does it better - simply!

If you go and take a quick look at the Laravel website; the first thing you will notice is the excellent documentation. A simple description of each function and comprehensive API.

What is missing for me is a sample application or some tutorials. So what I plan to do here is describe building my first simple Laravel application.

Installation and Configuration

Download and Install

Download the latest version from laravel.com and follow the instructions.

Config

To test that everything is working you only need to change one line in the /application/config/application.php file.

Just set the url to your site path, mine is:

	
'url' => 'http://localhost/laravel1/public',

Just open your browser and navigate to the application home page, and you should be up and running.

Views

Standard layout

It is easy to set up a template, so that your views only contain the information and data relevant to the specific page.

more ...

Forms

Is it better to embed the PHP in the HTML or embed the HTML in the PHP?

In the past I have always tried to make sure that php files containing HTML are primarily HTML with small injections of PHP, thus:

<p>
  <label for="email">Email:</label>
  <input type="text" name="email" id="email"
           class="<?php echo $person->hasError('email');?>"
           value="<?php echo $person->get('email'); ?>"/>
  <span class="error">
    <?php echo $person->hasError('email', true); ?>
  </span>
</p>

My first attempt with Laravel looked something like this:

<p>
  <?php echo Form::label('surname', 'Surname'); ?>
  <?php echo Form::text('surname', $user->surname); ?>
  <span class='error'>
   <?php echo $user->hasError('surname'); ?>
  </span>
</p>

But because Laravel has such an elegant syntax for expressing form components I have changed my forms to look like this:

<?php
  // Surname
  echo '<p>'.Form::label('surname', 'Surname');
  echo Form::text('surname', $user->surname);
  echo '<span class="error">'.
    $user->hasError('surname').
    '</span></p>';
?>

I think this is much easier to read, and allows the use of standard PHP constructs within the code. It means that my non-form views are different in that they are mainly HTML with small bits of PHP, but for forms, I prefer this approach. What do you think?

TinyMCE

To use TinyMCE for rich text editing is a snip. Download the latest version of TinyMCE, drop the folder into the Laravel applications/libraries folder, paste the sample code in the <head> section of the default.php view and it works.


<<  back