Skip to content

duminhtam/laravel

Repository files navigation

Chotot.vn - Web developer recruitment exercises

Base on Chotot assignments.pdf

Requirement

The Laravel framework has a few system requirements:

  • PHP >= 5.3.7
  • MCrypt PHP Extension
  • Apache Webserver
  • CURL with PHP-CURL Extension

And some of external libraries (included by composer.json file) use in both dev and production environments:

####require

####require-dev

####Libraries

####Javascript

Installation

Composer

Install Composer, take a look at composer installation guide

follow these step
  • step 1 - install composer by download the composer.phar
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/root
  • step 2 - install laravel 4 with composer
    git clone git://github.com/duminhtam/laravel.git laravel cd laravel
    composer install --dev this will install with dev packages
    'Basset\BassetServiceProvider'
    'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider'
    'Way\Generators\GeneratorsServiceProvider'
    'Way\Console\GuardLaravelServiceProvider'

and aliases also (in app.php bottom)

'Basset' => 'Basset\Facade' ### Configuration Change the owner of all file and folder to apache:root

chown -R apache:root laravel

Apache virtual host configuration

My project root is located at /apache/laravel and the virtual host DocumentRoot must be pointed to /apache/laravel/public

NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /apache/laravel/public
ServerName laravel.local
<Directory "/apache/laravel/public"> 
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
</Directory>
</VirtualHost>

Linux Command Alias

Some linux command alias that will be used in my readme file my composer.phar is located in /root/composer.phar

    alias composer='php /root/composer.phar'
    alias artisan='php artisan'
    alias phpunit='test'

These command only work when you are at the project root file, my project root apache/laravel, use pwd to view your current work directory. Unless you are in your document root, go to it directory with command cd /apache/laravel

####Compile CSS and JS This will create script and css to compiled folder

artisan basset:build -p chotot

Configuration is located in

app/config/packages/jasonlewis/basset/config.php

Environment

The default environment is production, you can keep this to run without complex config or you can configure as local, take a look at laravel environment configuration (sorry I dont have enough time for document it).
Change and view machine name tutorial if use want to change environment.

Database

You can you any database you want with laravel support, please config in app/config/database.php

'default' => 'sqlite', I use sqlite, so that no configuration needed. The database file is located at app/database/production.sqlite production does not mean the environtment, it is only the file name in configuration bellow:. ```php 'sqlite' => array( 'driver' => 'sqlite', 'database' => __DIR__.'/../database/production.sqlite', 'prefix' => '', ), ```

Other database support:

mysql, pgsql, sqlsrv

A sample mysql config if you want to:

    'mysql' => array(
        		'driver'    => 'mysql',  
    			'host'      => 'localhost',  
    			'database'  => 'ads',  
    			'username'  => 'ads_user',  
    			'password'  => 'qweQWE123!!!',  
    			'charset'   => 'utf8',  
    			'collation' => 'utf8_unicode_ci',  
    			'prefix'    => '',  
    		),  

and change the default database engine to mysql in app/config/database.php

'default' => 'mysql', #### Migration Use artisan to migrate your database, this will create(database table, fields, index, view) artisan migrate

The migration schema file located in app/config/database/migrations/2013_08_10_061829_create_ads_table.php file.

This will create the table with structured and view bellow:

    //file <b>2013_08_10_061829_create_ads_table.php</b>
    Schema::create('ads', function(Blueprint $table) {
    			$table->increments('id');
    			$table->string('title', 255);
    			$table->text('description');
    			$table->float('price', 10);
    			$table->string('currency', 3);
    			$table->tinyInteger('col', 2)->default(1);
    	     	$table->tinyInteger('row', 2)->default(1);
    			<b>$table->string('url', 255)->unique();</b> <i>//url with unique index, be used in <b>"new ads check"</b></i>
    			$table->string('date', 255);
    			$table->string('img', 255);
    			$table->string('category', 255);
    			$table->string('date_posted', 25);
                //create required indexes
                 $table->index('row');
                $table->index('col');
    			$table->timestamps();
    		});
            //create required views
            DB::statement('CREATE VIEW new_ads AS
                          SELECT *
                          FROM ads
                          ORDER BY id DESC;
                          ');

Database stucture image (sqlite):

db structure image

Change the owner of all file and folder to apache:root when run migration

chown -R apache:root laravel

JS config

The js configuration is located in app/models/ChoTot.php

    const CONFIG_MAX_COLS = 10; //col
    const CONFIG_RUN_INTERVAL = 1000; //ms
    const CONFIG_IDLE_INTERVAL = 5; //idle second

Running

All routes are configured in app/routes.php file.
Framework included index route still be kept

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

Index Route

Browser URL: /chotot

Image

chotot crawler index

Cron Route

This will crawl frist 20 ads result in chotot.vn /hochiminh and store in db, return the json result also

Browser URL: /chotot/cron

JSON result image

a

Update Route

This post route is filtered by csrf. The chotot/update route only accept post method with params described bellow:

{_token,ads:{ id,position } }

_token is the csrf token name

ads is the ads array with id and position, this is one update query for all object when it was arranged, no loop update(performance query).

Route code in app/routes.php:

Route::group(array('before' => 'csrf'), function()
{
    Route::post('chotot/update', 'ChoTotController@postUpdate');
});

Browser URL: /chotot/update

Testing

Run test alias of phpunit command from document root to get the test result.