Phalcon - Giant Killer
-
Upload
jackson-f-de-a-mafra -
Category
Technology
-
view
526 -
download
5
description
Transcript of Phalcon - Giant Killer
![Page 1: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/1.jpg)
PhalconGiant Killer
![Page 2: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/2.jpg)
Jacksonfdam
http://about.me/jacksonfdam
https://bitbucket.org/jacksonfdam
https://github.com/jacksonfdam
http://linkedin.com/in/jacksonfdam
@jacksonfdam
![Page 3: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/3.jpg)
A thanks to
For encourage and believe in the passage of knowledge. (And to release me from work to be here.)”
![Page 4: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/4.jpg)
Why?
• Use of frameworks has become mandatory in professional development with PHP • They offer a philosophy and organized structures to easily maintain projects writing less code and making work more fun
![Page 5: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/5.jpg)
How PHP works?
1. PHP have dynamic and weak typing discipline 2. PHP is interpreted (not compiled) 3. Every time a script is loaded, should be interpreted by PHP 4. If a bytecode cache (like APC) isn’t used, syntax checking is performed every time
![Page 6: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/6.jpg)
How traditional php frameworks works?
1. Many files with classes and functions are read at every request made. Disk reading is expensive in terms of performance 2. Modern frameworks use lazy loading technique (autoload) for load and execute only code needed 3. Continuous loading/interpreting could be expensive and impact your application performance 4. When you use a framework most of the code remain the same across development. Why load and interpret it every time?
![Page 7: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/7.jpg)
How a PHP C extension works?
1. C extensions are loaded together with PHP one time, on the web server dispatch process 2. Classes and functions provided by the extension are ready to use for any application 3. The code isn’t interpreted because it is compiled to a specific platform and processor
![Page 8: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/8.jpg)
What is Phalcon?
Phalcon is an open source, full stack framework for PHP 5 written as a C-extension, optimized for high performance. You don’t need to learn or use the C language, since the functionality is exposed as PHP classes ready for you to use. Phalcon also is loosely coupled, allowing you to use
its objects as glue components based on the needs of your application.
![Page 9: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/9.jpg)
How Phalcon works?
1. Components are loosely coupled. You may use the components you want without depending on a full stack 2. Low-level optimizations provide the lowest overhead for MVC-based applications 3. Interact with databases with maximum performance by using a C-language ORM for PHP 4. Phalcon is directly engaged with PHP, so it can directly access internal structures optimizing execution as well
![Page 10: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/10.jpg)
What is Phalcon?
![Page 11: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/11.jpg)
Compare how Phalcon is faster than other frameworks
Traditional Frameworks Phalcon
Interpretation of hundreds or thousands of PHP lines on each request
YES NO
Load dozens of files in each request
YES NO
Checking the existence of functions, methods, classes, constants at each request
YES NO
Low-level optimizations gaining microseconds for each request
NO YES
![Page 12: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/12.jpg)
Framework Benchmark
![Page 13: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/13.jpg)
Framework Benchmark
![Page 14: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/14.jpg)
Framework Benchmark
<h1>Hello!</h1>
![Page 15: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/15.jpg)
Test Environment
Operative System: Mac OS X Snow Leopard 10.6.8 • Web Server: Apache httpd 2.2.21 • PHP: 5.3.8 + APC 3.1.9 (Without Xdebug) • CPU: 3.06 Ghz Intel Core 2 Duo • Main Memory: 4GB 1067 MHz DDR3 • Hard Disk: 500GB SCSI/SAS HDD
![Page 16: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/16.jpg)
Framework Benchmark
ab -n 1000 -c 5 http://localhost/bench/phalcon
![Page 17: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/17.jpg)
Framework/Requests per Second
![Page 18: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/18.jpg)
Framework Total Requests/Average Time
![Page 19: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/19.jpg)
Number of Interpreted Files per Request
![Page 20: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/20.jpg)
Allocated Memory per Request
![Page 21: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/21.jpg)
Installation
To use phalcon on Windows you can download a DLL library. Edit your php.ini file and then append at the end:
extension=php_phalcon.dllRestart your webserver.
![Page 22: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/22.jpg)
Requirements
Prerequisite packages are:
PHP >= 5.3 development resourcesGCC compiler (Linux/Solaris)Git (if not already installed in your system - unless you download the package from GitHub and upload it on your server via FTP/SFTP)
![Page 23: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/23.jpg)
Requirements
Prerequisite packages are:
PHP >= 5.3 development resourcesGCC compiler (Linux/Solaris)Git (if not already installed in your system - unless you download the package from GitHub and upload it on your server via FTP/SFTP)
![Page 24: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/24.jpg)
Requirements
Specific packages for common platforms:
#Ubuntusudo apt-get install php5-dev libpcre3-dev gcc make php5-mysql
#Susesudo yast -i gcc make autoconf2.13 php5-devel php5-pear php5-mysql
#CentOS/RedHat/Fedorasudo yum install php-devel pcre-devel gcc make
#Solarispkg install gcc-45 php-53 apache-php53
![Page 25: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/25.jpg)
Compilation
git clone --depth=1 git://github.com/phalcon/cphalcon.gitcd cphalcon/buildsudo ./install
![Page 26: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/26.jpg)
Configure
Add extension to your php configuration:#Suse: Add this line in your php.iniextension=phalcon.so
#Centos/RedHat/Fedora: Add a file called phalcon.ini in /etc/php.d/ with this content:extension=phalcon.so
#Ubuntu/Debian: Add a file called 30-phalcon.ini in /etc/php.d/ with this content:extension=phalcon.so
#Debian with php5-fpm: Add a file called 30-phalcon.ini in /etc/php5/fpm/conf.d/30-phalcon.ini with this content:extension=phalcon.so
Restart the webserver.
![Page 27: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/27.jpg)
Configure
Phalcon automatically detects your architecture, however, you can force the compilation for a specific architecture:
cd cphalon/buildsudo ./install 32bitssudo ./install 64bitssudo ./install safe
![Page 28: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/28.jpg)
Mac OS X
On a Mac OS X system you can compile and install the extension from the source code:
RequirementsPrerequisite packages are:PHP >= 5.3 development resourcesXCode#brewbrew tap homebrew/homebrew-phpbrew install php53-phalconbrew install php54-phalconbrew install php55-phalconbrew install php56-phalcon
#MacPortssudo port install php53-phalconsudo port install php54-phalconsudo port install php55-phalconsudo port install php56-phalcon
![Page 29: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/29.jpg)
Installation on XAMPP
XAMPP is an easy to install Apache distribution containing MySQL, PHP and Perl. Once you download XAMPP, all you have to do is extract it and start using it. Below are detailed instructions on how to install Phalcon on XAMPP for Windows. Using the latest XAMPP version is highly recommended.
![Page 30: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/30.jpg)
Installation on XAMPP
XAMPP is always releasing 32 bit versions of Apache and PHP. You will need to download the x86 version of Phalcon for Windows from the download section.
After downloading the Phalcon library you will have a zip file like the one shown below:
![Page 31: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/31.jpg)
Installation on XAMPP
Extract the library from the archive to get the Phalcon DLL:
![Page 32: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/32.jpg)
Installation on XAMPP
Copy the file php_phalcon.dll to the PHP extensions. If you have installed XAMPP in the c:\xampp folder, the extension needs to be in c:\xampp\php\ext
![Page 33: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/33.jpg)
Installation on XAMPP
Edit the php.ini file, it is located at C:\xampp\php\php.ini. It can be edited with Notepad or a similar program. We recommend Notepad++ to avoid issues with line endings. Append at the end of the file: extension=php_phalcon.dll and save it.
![Page 34: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/34.jpg)
Installation on XAMPP
Restart the Apache Web Server from the XAMPP Control Center. This will load the new PHP configuration.
![Page 35: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/35.jpg)
Installation on XAMPP
Open your browser to navigate to http://localhost. The XAMPP welcome page will appear. Click on the link phpinfo().
phpinfo() will output a significant amount of information on screen about the current state of PHP. Scroll down to check if the phalcon extension has been loaded correctly.
If you can see the phalcon version in the phpinfo() output, congrats!, You are now flying with Phalcon.
![Page 36: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/36.jpg)
Creating a project
File structurePhalcon does not impose a particular file structure for application development. Due to the fact that it is loosely coupled, you can implement Phalcon powered applications with a file structure you are most comfortable using.
![Page 37: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/37.jpg)
Creating a project
For the purposes of this tutorial and as a starting point, we suggest the following structure:tutorial/ app/ controllers/ models/ views/ public/ css/ img/ js/
![Page 38: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/38.jpg)
Beautiful URLs
#/tutorial/.htaccess<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L]</IfModule>
#/tutorial/.htaccess<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L]</IfModule>
![Page 39: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/39.jpg)
Bootstrap<?php
try {
//Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerDirs(array( '../app/controllers/', '../app/models/' ))->register();
//Create a DI $di = new Phalcon\DI\FactoryDefault();
//Setup the view component $di->set('view', function(){ $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); return $view; });
//Setup a base URI so that all generated URIs include the "tutorial" folder $di->set('url', function(){ $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/tutorial/'); return $url; });
//Handle the request $application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
} catch(\Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage();}
![Page 40: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/40.jpg)
Creating a Controller
By default Phalcon will look for a controller named “Index”. It is the starting point when no controller or action has been passed in the request. The index controller (app/controllers/IndexController.php) looks like:
<?phpclass IndexController extends \Phalcon\Mvc\Controller{ public function indexAction() { echo "<h1>Hello!</h1>"; }}
![Page 41: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/41.jpg)
Creating a Controller
Sending output to a viewSending output to the screen from the controller is at times necessary but not desirable as most purists in the MVC community will attest. Everything must be passed to the view that is responsible for outputting data on screen. Phalcon will look for a view with the same name as the last executed action inside a directory named as the last executed controller. In our case (app/views/index/index.phtml):
<?php echo "<h1>Hello!</h1>";Our controller (app/controllers/IndexController.php) now has an empty action definition:<?phpclass IndexController extends \Phalcon\Mvc\Controller{ public function indexAction() { }
}
![Page 42: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/42.jpg)
Creating a Model
Phalcon brings the first ORM for PHP entirely written in C-language. Instead of increasing the complexity of development, it simplifies it.
Before creating our first model, we need to create a database table outside of Phalcon to map it to. A simple table to store registered users can be defined like this:
CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(70) NOT NULL, `email` varchar(70) NOT NULL, PRIMARY KEY (`id`));
![Page 43: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/43.jpg)
Creating a Model
A model should be located in the app/models directory (app/models/Users.php). The model maps to the “users” table:
<?php
class Users extends \Phalcon\Mvc\Model{
}
![Page 44: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/44.jpg)
Setting a Database Connection
In order to be able to use a database connection and subsequently access data through our models, we need to specify it in our bootstrap process. A database connection is just another service that our application has that can be used for several components:
![Page 45: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/45.jpg)
Setting a Database Connection<?php
try {
//Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerDirs(array( '../app/controllers/', '../app/models/' ))->register();
//Create a DI $di = new Phalcon\DI\FactoryDefault();
/
/Setup the database service $di->set('db', function(){ return new \Phalcon\Db\Adapter\Pdo\Mysql(array( "host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "test_db" )); });
//Setup the view component $di->set('view', function(){ $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); return $view; });
//Setup a base URI so that all generated URIs include the "tutorial" folder $di->set('url', function(){ $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/tutorial/'); return $url; });
//Handle the request $application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
} catch(Exception $e) { echo "PhalconException: ", $e->getMessage();}
![Page 46: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/46.jpg)
Volt: Template Engine
Volt is an ultra-fast and designer friendly templating language written in C for PHP. It provides you a set of helpers to write views in an easy way. Volt is highly integrated with other components of Phalcon, just as you can use it as a stand-alone component in your applications.
![Page 47: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/47.jpg)
Volt: Template Engine
![Page 48: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/48.jpg)
Volt: Template Engine
Volt views are compiled to pure PHP code, so basically they save the effort of writing PHP code manually:
{# app/views/products/show.volt #}{% block last_products %}{% for product in products %}
* Name: {{ product.name|e }}
{% if product.status == "Active" %}
Price: {{ product.price + product.taxes/100 }}
{% endif %}{% endfor %}{% endblock %}
![Page 49: Phalcon - Giant Killer](https://reader034.fdocuments.net/reader034/viewer/2022052322/5582a726d8b42a584c8b45bd/html5/thumbnails/49.jpg)
Thank you.@jacksonfdam