Difference between revisions of "6CS028 Workshop - Web Frameworks"

From mi-linux
Jump to navigationJump to search
Line 1: Line 1:
 
[[Main Page]] >> [[6CC001|Advanced Web Technologies]] >> [[6CC001 - Workbook|Workbook]] >> Week 02
 
[[Main Page]] >> [[6CC001|Advanced Web Technologies]] >> [[6CC001 - Workbook|Workbook]] >> Week 02
  
Consider using a PHP framework for your assessment. Here are some popular ones:
+
== Codeigniter==
  
==Summary of findings==
+
Please install Codeigniter and work through the following tutorials:
 +
[https://codeigniter.com/user_guide/tutorial/index.html https://codeigniter.com/user_guide/tutorial/index.html]
  
*<span style="color:#00C600">CodeIgniter</span>: Possibly the easiest one the learn first (flexible, no command line)
+
=== External files (images, css files, js files) ===
*<span style="color:#00C600">Yii</span>: A bit more tricky as you need to run a few commands (to create an app, to use scaffolding)
 
*<span style="color:#00C600">CakePHP</span>: Quite simple to install (no command line required), but might be a bit more difficult to learn (I can't get mod rewrite to work at the moment)
 
*<span style="color:#00C600">Laravel</span>: Working fine, although installation is not straight forward.
 
*<span style="color:#FFC601">Symfony</span>: Difficult to set-up (see my notes) and seems to be using a lot of RAM on the server :/
 
*<span style="color:#FF0000">Zend</span>: I can't get this to work on mi-linux
 
 
 
==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 [http://www.chiark.greenend.org.uk/~sgtatham/putty/ 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.
 
 
 
===Useful commands===
 
 
 
To make a whole folder + sub-folders readable and executable by all:
 
<pre>
 
chmod 755 codeigniter/ -R
 
</pre>
 
 
 
To remove a whole folder (WARNING - CANNOT BE UNDONE)
 
<pre>
 
rm codeigniter/ -R
 
</pre>
 
 
 
==Link to external CSS, JS, Images etc==
 
 
 
=== Codeigniter===
 
  
 
First make sure your base URL is populated in your config.php file, so for example for me:
 
First make sure your base URL is populated in your config.php file, so for example for me:
Line 63: Line 35:
 
</p>   
 
</p>   
 
</pre>
 
</pre>
 
==CodeIgniter ==
 
 
I tested version 3.0:
 
*[http://www.codeigniter.com/ Framework's homepage]
 
 
First install CodeIgniter using this tutorial:
 
*[http://www.codeigniter.com/user_guide/installation/index.html Installation Instructions]
 
 
If successfully installed you should see this page:
 
*[http://mi-linux.wlv.ac.uk/~in9352/codeigniter/ http://mi-linux.wlv.ac.uk/~in9352/codeigniter/]
 
 
Note: replace "in9352" with your student number and "codeigniter" with the folder name in which you have installed the framework.
 
 
Then work through the first few tutorials:
 
*[http://www.codeigniter.com/user_guide/tutorial/static_pages.html Static pages]
 
*[http://www.codeigniter.com/user_guide/tutorial/news_section.html News section]
 
*[http://www.codeigniter.com/user_guide/tutorial/create_news_items.html Create news items]
 
 
Nothing too complicated in the tutorials above, however '''I 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:
 
 
<pre>
 
[your-site-url]index.php/controller/method
 
</pre>
 
 
So far example:
 
 
<pre>
 
http://mi-linux.wlv.ac.uk/~in9352/codeigniter/index.php/pages/view
 
</pre>
 
 
==Yii==
 
 
*[http://www.yiiframework.com/download/ Yii homepage]
 
*[http://www.yiiframework.com/doc-2.0/guide-start-installation.html Getting started]
 
 
Unfortunatly the brand new Yii 2.0 release requires PHP 5.4.0 or above, and the version currently installed on mi-linux is 5.3.10 :(
 
 
You could always work with version 1 of the framework, but it seems a shame, as it won't receive any new features.
 
 
==CakePHP==
 
 
I tested version 2.4.5
 
 
*[http://cakephp.org/ CakePHP homepage]
 
*[http://book.cakephp.org/2.0/en/getting-started.html Getting started tutorial]
 
 
===mod_rewrite===
 
 
IMPORTANT: I can't get MOD REWRITE to work at the moment, so my clean URL doesn't work:
 
*[http://mi-linux.wlv.ac.uk/~in9352/cakephp/posts/index http://mi-linux.wlv.ac.uk/~in9352/cakephp/posts/index]
 
 
Instead I have to use the full URL, which is not great:
 
*[http://mi-linux.wlv.ac.uk/~in9352/cakephp/app/webroot/index.php/posts/index http://mi-linux.wlv.ac.uk/~in9352/cakephp/app/webroot/index.php/posts/index]
 
 
I'll update you if I get it working :)
 
 
===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.
 
 
===PHP markers===
 
 
Don't forget the <?php markers at the beginning of your Conrtoller and Model files, they are omitted in the tutorial.
 
 
==Symfony==
 
 
I tested version 2.4.1:
 
*[http://symfony.com/ Framework's homepage]
 
*[http://symfony.com/doc/current/quick_tour/the_big_picture.html Quick tour]
 
 
Note: download the zip version of Symfony Standard.
 
 
===Set-up===
 
 
I found this framework quite difficult to set-up.
 
 
First I had to open the symfony/web/app_dev.php file to comment out this block of code:
 
 
<pre>
 
// This check prevents access to debug front controllers that are deployed by accident to production servers.
 
// Feel free to remove this, extend it, or make something more sophisticated.
 
/*if (isset($_SERVER['HTTP_CLIENT_IP'])
 
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
 
    || !in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
 
) {
 
    header('HTTP/1.0 403 Forbidden');
 
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
 
}*/
 
</pre>
 
 
And then again in symfony/web/config.php
 
 
<pre>
 
/*if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
 
    '127.0.0.1',
 
    '::1',
 
))) {
 
    header('HTTP/1.0 403 Forbidden');
 
    exit('This script is only accessible from localhost.');
 
}*/
 
</pre>
 
 
Now I can browse to the [http://mi-linux.wlv.ac.uk/~in9352/symfony/web/config.php config.php] file, which tells me I need to make the folders app/cache/ and app/logs/ writable by all, which is easy enough.
 
 
It also tells me to "set the date.timezone setting in php.ini (like Europe/Paris)". I will ask ITS to make the change at server level, but in the meantime you can specify it in the .htaccess file, by adding this line right at the end of the file:
 
 
<pre>
 
php_value date.timezone "Europe/London"
 
</pre>
 
 
I can now browse to my home page ([http://mi-linux.wlv.ac.uk/~in9352/symfony/web/app_dev.php/ http://mi-linux.wlv.ac.uk/~in9352/symfony/web/app_dev.php/]), but it throws a memory error:
 
 
<pre>
 
FatalErrorException: Error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 262144 bytes)
 
</pre>
 
 
Again we can get around the issue by increasing the amount of RAM PHP can use on the server by adding this line to the .htaccess file:
 
 
<pre>
 
php_value memory_limit 64M
 
</pre>
 
 
But I am a little concerned that this framework seems to require a lot of server resources!
 
 
==Laravel==
 
 
*[http://laravel.com/ Homepage]
 
*[http://laravel.com/docs/quick Quickstart guide]
 
 
===Installing Laravel===
 
 
The Laravel framework utilizes [https://getcomposer.org/ Composer] for installation and dependency management, which is not currently installed on mi-linux. Fortunately you can install it in your home folder by running the following commands:
 
 
<pre>
 
mkdir ~/bin
 
wget -O ~/bin/composer https://getcomposer.org/composer.phar
 
chmod +x ~/bin/composer
 
echo PATH=\$PATH:\~/bin >>~/.bashrc
 
. ~/.bashrc
 
</pre>
 
 
Then you can run the install command as per [http://laravel.com/docs/quick Quickstart guide] instructions:
 
 
<pre>
 
composer create-project laravel/laravel your-project-name --prefer-dist
 
</pre>
 
 
The command above will download and install a fresh copy of Laravel in a new '''your-project-name''' folder within your current directory. I want to call my folder "laravel", so I move to public_html, and run the following command:
 
 
<pre>
 
composer create-project laravel/laravel laravel --prefer-dist
 
</pre>
 
 
The installation takes a while. Once finished I shoule be able to browse to the newly created app's public folder:
 
[http://mi-linux.wlv.ac.uk/~in9352/laravel/public/index.php http://mi-linux.wlv.ac.uk/~in9352/laravel/public/index.php]
 
 
But at first I was getting an HTTP 500 error. This is normally caused by invalid/unsupported commands in the .htaccess file.
 
 
I had to REMOVE the following lines from the .htaccess located in laravel/public
 
 
<pre>
 
<IfModule mod_negotiation.c>
 
    Options -MultiViews
 
</IfModule>
 
</pre>
 
 
Now I can browse to the welcome page:
 
[http://mi-linux.wlv.ac.uk/~in9352/laravel/public/index.php http://mi-linux.wlv.ac.uk/~in9352/laravel/public/index.php] (it should say "You have arrived").
 
 
===Getting started===
 
 
Now you can get on with the Quickstart guide (see URL above), for example if I add this "route" to the routes.php file as per instructions:
 
 
<pre>
 
Route::get('users', function()
 
{
 
    return 'Users!';
 
});
 
</pre>
 
 
I can then browse to:
 
[http://mi-linux.wlv.ac.uk/~in9352/laravel/public/index.php/users http://mi-linux.wlv.ac.uk/~in9352/laravel/public/index.php/users]
 
 
Ideally we would want to hide the "index.php" in the URL, but we'll worry about this later :)
 
 
==Zend Framework==
 
 
So far I have not managed to successfully install Zend on mi-linux :(
 

Revision as of 14:59, 21 October 2015

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

Codeigniter

Please install Codeigniter and work through the following tutorials: https://codeigniter.com/user_guide/tutorial/index.html

External files (images, css files, js files)

First 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/';

Then upload your files in your "codeigniter" folder, or in suitable sub-folders (codeigniter/images, codeigniter/css and codeigniter/js). Do NOT upload the files into your codeigniter/application folder or any of its sub folders.

In the example below I have uploaded an image and a CSS file at the following locations:

  • codeigniter/css/style.css
  • codeigniter/funnycat.jpg

Finally use the "Base URL" helper in your view:

<?$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>