6CS028 Workshop - Web Frameworks

From mi-linux
Revision as of 10:51, 31 January 2014 by In9352 (talk | contribs)
Jump to navigationJump to search

Main Page >> Advanced Web Technologies >> Workbook >> Week 02

Consider using a PHP framework for your assessment. Here are some popular ones:

Summary of findings

Work in progress

Command line

Some of the frameworks expect you to type commands, using the command line. Don't let it put you off, unless you know absolutely NOTHING about Linux commands.

From Windows simply start "Putty", installed in the all the labs, and free to download from home.

When starting Putty simply type "mi-linux.wlv.ac.uk" in the "host name" box, and press "enter". Then log in using your normal details.

CodeIgniter

I tested version 2.1.4

Nothing too complicated in the tutorial above, however I would recommend that you skip the "Routing" section for now, as it make the format of the URL more confusing. Stick to the standard format for now:

[your-site-url]index.php/pages/view

So far example:

http://mi-linux.wlv.ac.uk/~in9352/codeigniter/index.php/pages/view

Go straight to the "News section" exercise, which will teach you about database access... much more important!

Link to external CSS, JS, Images etc

Make sure your base URL is populated in your config.php file, so for example for me:

$config['base_url']='http://mi-linux.wlv.ac.uk/~in9352/codeigniter/';

And then in your view use the URL helper:

<?$this->load->helper('url');?>
<html>
<head>
	<title><?php echo $title ?> - CodeIgniter 2 Tutorial</title>
  <link rel="stylesheet" type="text/css" href="<?=base_url("css/style.css")?>">
</head>
<body>
	<h1>CodeIgniter 2 Tutorial</h1>
<p>
<img src="<?=base_url("funnycat.jpg")?>">
</p>  

Yii

I tested version 1.1.13

Ignore the "Apache and Nginx configurations" step for now and go straight to the "Creating First Yii Application" step.

Create first app

The tutorial tells you to create your first project by running the following command (see above for more on how to run commands):

% YiiRoot/framework/yiic webapp WebRoot/myapp

The above worked for me, but one students tells me he is getting a "permission denied" error, which might mean you are not allowed to run bash commands. Not to worry, the script above also comes as a PHP script, so you can run this instead:

php YiiRoot/framework/yiic.php webapp WebRoot/myapp

Obviously in both cases you need to specify the paths as per your folders.

When browsing to your newly created application you might get the odd error message telling you to make certain folders writable.

Mysql Database

By default Yii is set up to access an SQLite database. If you want to use Mysql you will need to edit the protected/config/main.php file, uncomment the following block and add your usual Mysql connection details:

'db'=>array(
	'connectionString' => 'mysql:host=localhost;dbname=XXX',
	'emulatePrepare' => true,
	'username' => 'XXX',
	'password' => 'XXX',
	'charset' => 'utf8',
),

Gii

Gii is a new tool that allows you to generate code from the database (aka Scaffolding in the lecture slides). You enable it by uncommenting the following block in the protected/config/main.php file.

'gii'=>array(
	'class'=>'system.gii.GiiModule',
	'password'=>'1234',
	// If removed, Gii defaults to localhost only. Edit carefully to taste.
	'ipFilters'=>array('134.220.251.99','::1'),
),

Note: you need to specify your own password. Also I've had to put my IP address in the last field, or else the page was telling me I wasn't allowed in! On Windows you can get your IP address by opening a command line window and typing the command "ipconfig".

Other than that this seems to work quite well :) Obviously Gii can only create the php file if you make the folders writable (e.g. to generate a model class, you need to make the protected/models folder writable!)

CakePHP

I tested version 2.3.0

mod_rewrite

Note that because of the way mod_rewrite is set up on the server you need to add a RewriteBase line in the existing .htaccess file, as follow:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteBase /~0123456/cake
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

IMPORTANT: Replace "0123456" with your student number and "cake" by the folder in which you have uploaded the framework.

Alternatively you could NOT use mod_rewrite, the tutorial above tells you how (see "A Note on mod_rewrite" section)

Cache

If you are getting the following error message:

Warning: _cake_core_ cache was unable to write 'cake_dev_en-gb' to File cache in /studhome/staff/acad/in9352/public_html/cake/lib/Cake/Cache/Cache.php on line 309

You need to make the "app/tmp" folder (and all its sub-folders) writable by all in order to solve this.

If you are still getting cache errors/warnings after this, it might be because the version of PHP running on the server is too old, and caching doesn't work (we have 5.2.4 and the documentation says it should be 5.2.8 or more). You could always disable the cache in the Config/core.php file. Simply remove the following blocs located at the bottom of the file.

/**
 * Configure the cache used for general framework caching.  Path information,
 * object listings, and translation cache files are stored with this configuration.
 */
Cache::config('_cake_core_', array(
	'engine' => $engine,
	'prefix' => $prefix . 'cake_core_',
	'path' => CACHE . 'persistent' . DS,
	'serialize' => ($engine === 'File'),
	'duration' => $duration
));

/**
 * Configure the cache for model and datasource caches.  This cache configuration
 * is used to store schema descriptions, and table listings in connections.
 */
Cache::config('_cake_model_', array(
	'engine' => $engine,
	'prefix' => $prefix . 'cake_model_',
	'path' => CACHE . 'models' . DS,
	'serialize' => ($engine === 'File'),
	'duration' => $duration
));

Not ideal though :/

Symfony

I tested version 2.1.7

I get this right away:

ERROR
PHP version must be at least 5.3.3 (5.2.4-2ubuntu5.26 installed)
Install PHP 5.3.3 or newer (installed version is 5.2.4-2ubuntu5.26)

There might be ways around it, but frankly it is not very encouraging. I have logged a call with IT Services to get PHP updated... Wait and see :)

Akelos

I tested version 1.0.1

IN PROGRESS

After uploading the framework to the server I have run the following command:

./script/setup -d alix/ -p /home/staff/acad/in9352/public_html/akelos/alix

I got a few warnings (not sure why), and was told to finish the process by browsing to the newly created website.

When attempting to browse I received an HTTP 500 error. This is normally caused that commands in the .htaccess file that the server doesn't like. Removing the following bloc from all .htaccess files you find seems to work:

DirectoryIndex index.php

<IfModule sapi_apache2.c>
   php_value register_globals        0
   php_value magic_quotes_gpc        0
   php_value magic_quotes_runtime    0
   php_value magic_quotes_sybase     0
   php_value arg_separator.output    "&"
   php_value default_charset    "utf-8"
   php_value session.name           "AK_SESSID"
</IfModule>

<IfModule mod_php4.c>
   php_value register_globals        0
   php_value magic_quotes_gpc        0
   php_value magic_quotes_runtime    0
   php_value magic_quotes_sybase     0
   php_value arg_separator.output    "&"
   php_value default_charset    "utf-8"
   php_value session.name           "AK_SESSID"
</IfModule>

Problem: the install process requires an "alias" to be created in the .htaccess file, in order to redirect into the "public" folder when browsing to the root of your app, but as far as I can tell "aliases" are not permitted on mi-linux. This is an extract from the sample .htaccess file:

# Example:
#   Alias /myakelosapp /path/to/myakelosapp/public
# RewriteBase /framework

I am currently trying to implement the same redirect using mod_rewrite, but no luck so far.

If the above is fixed

Browse to your page and "run a step by step wizard for creating a configuration file". This seems to work fine at first (although as usual it will ask you to make a few folders writable so it can write to it). But at the end I got SQLite errors, even though I specified I wanted to use Mysql! Very confusing! I'll try creating the config file manually instead (after all it's how you do it in most frameworks)

The problem clearly lies in the alix/config/database.yml file - it contains SQLite details:

production:
        type: sqlite
        database_file: /studhome/staff/acad/in9352/public_html/akelos/alix/config/alix-5QScwe1O.sqlite
        host: 
        port: 
        database_name: 
        user: 
        password: 
        options: 

[...]

The tutorial doesn't tell you what needs to go in there for a Mysql database... a bit annoying, but I found the documentation eventually (going via Google!):

Also it assumes you want to use SQLite, and doesn't provide the SQL code to create the example database. Here it is for your convenience:

CREATE TABLE IF NOT EXISTS `books` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `description` longtext NOT NULL,
  `author_id` int(11) NOT NULL,
  `published_on` date NOT NULL,
  PRIMARY KEY  (`id`)
)

CREATE TABLE IF NOT EXISTS `authors` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
)

There, my empty site is working now (phew).

Next: let's use the scaffolding feature to generate the files (models, controllers etc.) from the database above!

Zend Framework

I tested version 2.1.1

IMPORTANT: So far I have not managed to successfully install Zend on mi-linux :(