You are viewing our old blog site. For latest posts, please visit us at the new space. Follow our publication there to stay updated with tech articles, tutorials, events & more.

Restful WebServices (Microservices) in Phalcon Framework

0.00 avg. rating (0% score) - 0 votes
About Phalcon:
 
Phalcon is a full-stack framework. It promotes the MVC architecture and offers features like an ORM, a Request object library, a templating engine, caching, pagination.
In short Phalcon:
  • Is a PHP C-extension based framework.
  • Is a C-Extensions are loaded together with PHP one time on the web server’s daemon start process
  • Is a classes and functions provided by the extension are ready to use for any application
  • Is a the code is compiled and isn’t interpreted because is already compiled to a specific platform and processor
 
The preliminary advantage of Phalcon from the fact that it’s entirely C-extension-based. The compiled C extensions are loaded at the beginning of web server’s process and then reside in RAM, allowing Phalcon to process over 2K+ requests per second on (Hexacore,16GB Machine) , nearly three times as many as CodeIgniter is able to manage(which is among the fastest framework available). Other than this aspect, however, it operates more or less the same as any other modern MVC-framework for PHP. It is one of the most appealing frameworks for your upcoming large scale, performance intensive projects.
 
 
Phalcon Installation requirements:
 
1) PHP >=5.3 development resources
notes : If you want to write UT’s then you need php version 5.4.19 . So that Phalcon 2.0 can be installed and incubator can be also installed. For incubator you need phalcon above verison 2.0 .
 
2)GCC Compiler (Linux) because Phalcon is written in c extension.
 
3) Git and composer
 
notes: So that you can clone from git .
 
Steps to install Phalcon :
 
1. steps for cloning and building:
 
        git clone –depth=1 git://github.com/phalcon/cphalcon.git

        cd cphalcon/build
 
        sudo ./install
 
2. Now Open Php.ini file add the line given below
 
        extension=phalcon.so
 
3. After this restart your server
 
Steps To Install Phalcon Webtool:
 
Using git (Direct Approach):
 
If you do not have a git account ,Please make account.
 
  1. git clone https://github.com/phalcon/phalcon-devtools.git
  2. ln -s /opt/phalcon-devtools/phalcon.php /usr/local/bin/phalcon
  3. chmod +x /usr/local/bin/phalcon
 
Installation using composer:
If you do not have composer install in your machine please follow these guide lines
First Install Composer using below steps:
  1. create directory in /opt/phalcon and go in that dirctory and create composer.json file.
    • mkdir /opt/phalcon && cd /opt/phalcon
    • vim composer.json.
  1. Add these lines in your composer.json file
    
    

     

    { “require”: { “phalcon/devtools”: “dev-master” } }
     
  1. Now execute following commands.
  • cd /opt/phalcon
  • composer install
  • ln -s /opt/phalcon/vendor/phalcon/devtools/phalcon.php /usr/local/bin/phalcon
  • chmod +x /usr/local/bin/phalcon
  1. To see which commands list phalcon devtool provides
    
    
    
    
     run phalcon commands
  1. To create a sample project using devtool
  • go to directory where you want to create project
  • run phalcon create-project myFirstApp
  1. after that you get a created project, Following would be the directory structure of your recently created project
myFirstApp/
 app/
    controllers/
    models/
    views/
  public/
    css/
    img/
    js/
    index.php
 
This is general directory structure for phalcon, but you want develop micro-services which 
can be used as plugin anywhere then I suggest follow directory structure.
myFirstApp/
      app/
    config/
    controllers/
    lib/
 manager/
 do/
 dao/
 common/
 factory/
 validator/
    core/
 bootstrap/
  public/
    css/
    img/
    js/
    index.php
Apache Phalcon installation guide:
 
  1. If you are using .htaccess file
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$ public/    [L]
    RewriteRule  ((?s).*) public/$1 [L]
</IfModule>
This re-writes all the URIs to the public/index.php file

<IfModule mod_rewrite.c>
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
By default index.php will go to Index controller and index action
  1. If you do not want to use .htaccess file Put these lines in your httpd.conf file
<IfModule mod_rewrite.c>
<Directory “/var/www/test”>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [L]
</Directory>

<Directory “/var/www/test/public”>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</Directory>
</IfModule>
For virtual host:

<VirtualHost *:80>
ServerAdmin admin@example.host
DocumentRoot “/var/www/myFirstApp/public”
DirectoryIndex index.php
ServerName example.host
ServerAlias www.example.host

<Directory “/var/www/myFirstApp/public”>
Options All
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
 
Controller class Code :
 
class IndexController extends ControllerBase
{
        public function indexAction() {
              echo “Hello Hari”;die;
        }
 
public function shyamAction() {
              echo “Hello Shyam”;die;
        }
}
 
Without any rewrite rule If you want to access index action

http://servername/index.php

 
Output:
Hello Hari
 
To access another action i.e. “shyam” the url will be like:
 
Output:
Hello Shyam
 
To access another controller action other than Default Index, url will be like:
 
 
With rewrite rule:
To access index action
http://servername/Index/index
 
To access Shyam action
http://servername/Index/shyam
 
To access other controller and other action in that controllers
http://servername/ControllerName/actionName
 
For More Details regarding Phalcon please refer below links: