Profile pic of Tommy KuTommy Ku's Blog

Reading and thinking

Create simple contact list application with Laravel and Openshift integration: Part 1

Posted on  by Tommy Ku

This post is more than 2 years old, it may contain outdated information

This is the part 1 of a 2-part tutorial about jump-starting a Laravel project on Openshift. go to part2

Laravel is a PHP web development framework providing nice and easy routing, convenient Blade templating, Artisan CLI for better project management and easy build powered by Composer.

In part 1, I will go through the steps of setting up a running Laravel on Openshift, which takes some effort to figure it out if you have never used Laravel and Composer before. In part 2, we will go deeper into creating a contact list application running on Openshift.

Show me the steps

  1. Deploying Laravel 4.1 with PHP 5.4 and MySQL 5.5

    If you use RHC cli

    rhc create app laravel php-5.4
    rhc cartridge add mysql-5.5 -a laravel
    

    If you use web console

    Choose Laravel 4.1 Quickstart when creating new application.

  2. Pull the repository into local environment by running git clone {address to the repo}

  3. Get Composer (if you have not had one), you can simply download the composer.phar if you do not want to install it.

  4. In console locate the repo folder, if you had done the global install run composer install, otherwise php composer.phar install. You will see /vendor folder being created with the autoload script inside.

  5. If you encounter an error like Script php artisan clear-compiled handling the post-install-cmd event return an error, try to ensure your PHP build has mcrypt installed.

After having done the 4 steps above, Composer would have downloaded the necessary dependencies and you should already have had Laravel setup and able to run locally. Go to http://localhost/{path-to-Laravel-app}/public/index.php

Finally!

However to run a production build we could do something more. These actions are perfectly optional but in part 2 you are assumpted to have done them.

Switch off debug mode. In app/config/app.php set the debug attribute to false. You can view the error log in app/storage/logs/laravel.log.

Set the URL to your app in app/config/app.php.

Relocate the public path out of /public. Copy every inside /public to repo root, then move everything else inside a new folder /Laravel.

You will have to modify /Laravel/bootstrap/paths.php

'public' => __DIR__.'/../..',

Also /index.php

require __DIR__.'/Laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/Laravel/bootstrap/start.php';

Modify the build hook to run Composer while publishing the source to Openshift.

In /.openshift/action_hooks/build remove the hash to uncomment the line

#( echo 'Installing/Updating Laravel'; unset GIT_DIR ; cd $OPENSHIFT_REPO_DIR/Laravel ; php $OPENSHIFT_DATA_DIR/composer.phar -q --no-ansi install )

Generate optimized class loader with php artisan optimize

What’s next?

For the basics of Laravel you should consult the documentation. Chinese version is also avaliable here.

Briefly speaking, to show the “hello world” page you saw above, you need to set a route, and implement the controller handling the request.

In /Laravel/app/routes.php

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

The controller is the function passed into Route::get, which define a path and its request handler. It simply render the template located at /Laravel/app/views/hello.php

You may define more routes like:

Route::get('/hello', "HomeController@hello");

Here HomeController@hello means the request handler is the hello() method defined in /Laravel/app/controllers/HomeController.php which is implemented as

public function hello()
{
  return "hello";
}

Go to http://localhost/{path-to-Laravel-app}/hello to see the effect.

Part 2

In part 1 we have a proper Laravel framework set up to start building the awesome contact list. In the forthcoming part 2 we will actually get into the details of building a frontend with HTML/CSS/AngularJS and the backend interacting with MySQL.

go to part 2


Reference

  1. Laravel 4.1 Quickstart | OpenShift by Red Hat
  2. Laravel - The PHP Framework For Web Artisans.
  3. Laravel 4 PHP Framework Documentation 繁體中文教學文件

You could also look at...

This post first appeared on . Permalink:

About the author

Profile pic of Tommy Ku

Tommy Ku, a Hong Kong-based Software Engineer experienced developing PHP and Java-based web solutions and passionate in Web technology.

Also a hobbyist digital and film photographer.