Generate migration from existed database in Laravel 4

Laravel supports migration function which help us manage database and collaborate with team members better. We write code to define schema, execute the migration to evolve the database schema.
Synchronize our source code with all team members, each person will knows about any scheme change. When you deloy for app on a production server , it's also a robust way to upgrade our database schema, I think.

I'm using Laravel with mySQL database. For quick database design, For some project, we need some database design first (It sounds  not in Agile way ^^). I use mySQL workbench and synchronize between local database and MySQL Workbench's EER model. We have EER diagram for document, live database in right place. And, we need migrate source in right place ,too.

Let's think about the work - flow in this situation:

For the first time.

ER analysis --> create EER diagram in mySQL workbench --> synchronize with dev database --> generate migration source code.
Change database schema when developing: 

Edit migration source --> migrate into DB --> synchronize with EER diagram in mySQL workbench to update EER diagram.

Thank to JeffreyWay  and Xethron who created  2 amazing tools which  help us generate migration source code from existed database in Laravel 4.

We need some config.

 Install Laravel 4 generator &  Laravel migrations Generator.

Edit your  project's composer.json and add an "require-dev" section:
 "require-dev":{
  "xethron/migrations-generator": "dev-master" 
 },
Run
composer update
You should see that xethron/migrations-generator has required way/generators package. way/generators
package is also installed.

Edit your config/app.php file (I recommend to create dev or local enviroment and make them separate from production )
  'Way\Generators\GeneratorsServiceProvider',
  'Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider',
Run php artisan command you would see generate command is added to migrate


Generate migration from existed database

Run php artisan migrate:generate command to generate migrate file of all tables in current connection.
It will creates migration files of all tables and foreign keys in your databases.

(To be continue ....)

Auto complete for Phalcon framework in Eclipse

Phalcon is a web framework implemented as a C extension , every Phalcon's fan know that. It mean we cannot view source code of framework like another PHP framework.

Thank for Phalcon Developer tool. It provide useful interface & class declaration scripts which support IDE auto complete function.

Assuming that, Phalcon developer tools  is installed.

In Eclipse, you just need to add devtool path : phalcon-devtools/ide/PHALCON_VERSION by adding external Source Folder in PHP include Path / Libraries tab.



Install Phalcon & Phalcon developer tool in Ubuntu 13.10

I installed  Phalcon on Ubuntu 13.10 from  Official manual and faced some problems may make beginner confused.

I.  install Phalcon 

First of all. I installed php development resources & GCC compiler:
sudo apt-get install php5-dev php5-mysql gcc
Download & install phalcon:

git clone --depth=1 git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install
Faced first error:
/usr/include/php5/ext/pcre/php_pcre.h:29:18: fatal error: pcre.h: No such file or directory #include "pcre.h" ^ compilation terminated. make: *** [phalcon.lo] Error 1 
After checking for a while, Phalcon need : libpcre3-dev pakage :
apt-get install libpcre3-dev
wee need add new config in additional .ini files/

Create phalcon.ini file in /etc/php5/apache2/conf.d/phalcon.ini add : extension=phalcon.so in this file. 

Reload your server. It works !

II. Install Phalcon developer tool 

After installing Phalcon and Phalcon developer tool successfully . We may got new message when try to use Phalcon developer tool by phalcon command
$ phalcon
ERROR: Phalcon extension isn't installed, follow these instructions to install it: http://docs.phalconphp.com/en/latest/reference/install.html

It means your php cli didn't realase phalcon framework (phalcon.so). Wee need to add phalcon.ini files in configuration file of PHP5 cli. First of all, find Configuration file Path by command
 php -i | grep "Configuration File"
#result:

Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini

#cd to Configuration Path
$ cd /etc/php5/cli/
$ /etc/php5/cli$ ls

#result
conf.d  php.ini
You can see that we have another php.ini file and conf.d folder in Configuration path. Add phalcon.ini which contain extension=phalcon.so into conf.d folder.

Try your  phalcon command again . It works !

Config Apache 2.4 in Ubuntu

From Ubuntu 12.04 LTS, Apche have new version 2.4 which is different from 2.2 and below.
The configuration hierarchy is listed in apache2.conf file 's comment.

# /etc/apache2/
# |-- apache2.conf
# | `--  ports.conf
# |-- mods-enabled
# | |-- *.load
# | `-- *.conf
# |-- conf-enabled
# | `-- *.conf
`-- sites-enabled
#   `-- *.conf

# * apache2.conf is the main configuration file (this file). It puts the pieces
#   together by including all remaining configuration files when starting up the
#   web server.
#
# * ports.conf is always included from the main configuration file. It is
#   supposed to determine listening ports for incoming connections which can be
#   customized anytime.
#
# * Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/
#   directories contain particular configuration snippets which manage modules,
#   global configuration fragments, or virtual host configurations,
#   respectively.
#
#   They are activated by symlinking available configuration files from their
#   respective *-available/ counterparts. These should be managed by 127.using our
#   helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See
#   their respective man pages for detailed information.
#
# * The binary is called apache2. Due to the use of environment variables, in
#   the default configuration, apache2 needs to be started/stopped with
#   /etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not
#   work with the default configuration.


I. How to change DocumentRoot in Apache 2.4

 I found that we need 3 steps:

1. Add more Directory  in  /etc/apache2/apache2.conf

<Directory YOUR_FOLDER>
 Options Indexes FollowSymLinks
 AllowOverride None
 Require all granted
</Directory>

2. Change default documentRoot in /etc/apache2/sites-available/000-default.conf :
   DocumentRoot /var/www

to

  DocumentRoot YOUR_FOLDER

3. Restart your Apache server : sudo service apache2 restart or  sudo /etc/init.d/apache2 restart

II. How to add virtualhost

We need 3 steps to complete this configure

 1. In /etc/apache2/sites-available folder  create SITENAME.conf file
( SITENAME is your name you want to set)
In SITENAME.conf  config your new virtual host
<VirtualHost *:80>
        DocumentRoot  YOUR_NEW_VIRTUAL_HOST_FOLDER
        ServerName YOUR_VIRTUAL_DOMAIN_NAME
        ServerAlias  YOUR_VIRTUAL_DOMAIN_ALIAS
        ServerAdmin webmaster@localhost

        ErrorLog ${APACHE_LOG_DIR}/error.log

        CustomLog ${APACHE_LOG_DIR}/access.log combinedme

</VirtualHost>

Remember : documentRoot should be configured before ServerName.

2. Run command : sudo a2ensite SITENAME.conf  
3. Restart your Apache server : sudo service apache2 restart or  sudo /etc/init.d/apache2 restart

※ Notes: Using debug command : bapache2ctl -S to get to know how your server is configured. 


You can see more how to config in this link

TCPDF ERROR: Some data has already been output, can't send PDF file

I faced an error when exporting PDF file:
TCPDF ERROR: Some data has already been output, can't send PDF file
and found the answer:
//Change To Avoid the PDF Error

  ob_end_clean();

//just before
$pdf->Output($filename, $output_dest);

Getting started with Nodejs

1.What is nodejs?

Node.js is a software package consisting of a JavaScript engine and a number asynchronous input / output (I/O) libraries. It allows you to run JavaScript on the command line, outside of your browser.

The JavaScript engine used is the V8 JavaScript engine, which also powers the Chrome Browser. The I/O libraries allow Node.js to interface with files or network devices in an asynchronous manner. This allows Node.js to be used as a fast, lightweight, event-based web server.

2.Why nodejs?

This is why nodejs section: add more after done something with nodejs

3.Nodejs learning resource

Tutorials
Videos
Screencasts
Books
Courses
Blogs
Podcasts
JavaScript resources
Node Modules
Other

Try nodejs

- Install nodejs via pakage manager : link

 References:
  • http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js?rq=1
  • http://www.stanford.edu/class/cs98si/slides/nodejs.html

Performance benchmark of popular PHP frameworks

A PHP framework gives you common structures to normalise web apps, assist productivity, reduce repetitive coding and create more stable sites. When a web application development team choose which framework they will use, there are a lot of things they have to make decision. Framework performance sometime become an important factor.
This entry which is copied from systemsarchitect.net help you know more about performance of popular php frameworks.
-----

There are many assumptions around performance of different PHP frameworks. I frequently hear strong opinions about superiority X over Y in this context. There are companies writing new PHP frameworks from scratch because available solutions are too slow for them. What does it really mean? Does the framework performance matters? Before answering this questions lets check how slow is your framework!

Performing a representative benchmark across different framework is not an easy task. There are multiple ways to use each of them. Every use case will give different reading. Lets take routing as an example. Zend1 by default doesn’t need a routing file. It’s happy to use “/controller/action” pattern. On the other hand Symfony2 comes with a routing configuration. The file has to be read and parsed. That obviously takes some additional CPU cycles but does it mean Symfony2 routing is slower then Zend1? The answer is (obviously) no.

I benchmarked “quick start” projects. That gives some idea on what is the base line for every framework and makes it possible to reproduce my tests (and argue against them).
Code was hosted on Amazon EC2 medium instance. I installed PHP-APC to avoid disc access and code parsing. I also made sure there is no I/O on Apache2 or application level. I set logs and cache paths to “/dev/shm/”. I tweaked projects to make them return roughly the same amount of data (10KB). All virtual hosts had the same mod_rewrite rules. AllowOveride was set to None.

Benchmarked frameworks:
  1. Cake PHP
  2. Code Igniter
  3. Fuel PHP
  4. Hazaar MVC
  5. Kohana
  6. Laravel
  7. Netter
  8. Phalcon
  9. Silex
  10. Slim Framework
  11. Symfony 2
  12. YII
  13. Zend Framework 1
  14. Zend Framework 2

Requests per second from Apache Benchamrk with c=20 and n=500.
Framework Req/Sec
Phalcon 822.96
Slim 399.83
Kohana 217.34
Code Igniter 187.78
Silex 179.01
Laravel 135.9
YII 123.5
Fuel PHP 116.34
Hazaar MVC 103.53
Zend 1 103.02
Cake PHP 54.97
Nette 53.48
Symfony2 39.22
Zend 2 36.1
I’m not surprised, Slim is the fastest because it’s a micro framework. The Quick Start project didn’t use any templates or layout which obviously contributed to the reading.
Zend1 is twice faster than Symfony2 and Zend2 but in my experience the number will quickly go down in a real live setup.
Frameworks should speed up development, performance is a secondary concern. Zend 2 and Symfony2 could do better but it’s not bad. There are ways to improve those numbers on production servers. Don’t reinvent the wheel, learn and use frameworks. There are various options which balance between performance and features.