Install the YAF framework in LNMP (linux+nginx+mysql+php) environment and write an MVC example

Source: Internet
Author: User
Tags fpm naming convention zts



1, download yaf Source package yaf-2.2.9.tar.gz, and upload it to the server specified location, and then unzip and install:



[Email protected] src]# TAR-XVF yaf-2.2.9.tar.gz



Enter the extracted directory:



[Email protected] src]# CD yaf-2.2.9



One execution:



[Email protected] yaf-2.2.9]#/usr/local/php/bin/phpize



[Email protected] yaf-2.2.9]#/configure--with-php-config=/usr/local/php/bin/php-config



[[email protected] yaf-2.2.9]# make && make install



2. View the compiled file:



[Email protected] ~]# ll/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/yaf.so



-rwxr-xr-x 1 root root 771610 Jul 8 18:13/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/yaf.so



you can see that the yaf.so php Extension has been compiled for us as well.



3, configuration php.ini



[Email protected] ~]# Vim/usr/local/php/lib/php.ini



extension=yaf.so// then load yaf.so into php.ini



4. Restart php-fpm



[Email protected] ~]# service PHP-FPM restart



Gracefully shutting down php-fpm. Done



Starting php-fpm Done



5. View phpinfo ()











3. Generate the code using the Fast Code generation tool YAF comes with yaf_code_generator :



1) Download yaf Toolkit, browse https://github.com/laruence/php-yaf, download source package php-yaf-yaf-2.2.9.tar.gzandupload it to the server at the specified location, then unzip:



[Email protected] src]# TAR-XVF php-yaf-yaf-2.2.9.tar.gz



Then go to the extracted directory:



[Email protected] src]# CD php-yaf-yaf-2.2.9



Then enter the tools/cg directory:



[Email protected] php-yaf-yaf-2.2.9]# CD tools/cg/



Then execute (the app is the generated directory name ):



[Email protected] cg]#/usr/local/php/bin/php YAF_CG app



Executing the above code will generate an yaf skeleton code app in the cg/output/ directory



2) Copy the generated app skeleton code to the nginx HTML (Web root directory) directory:



[Email protected] ~]# cp-r/usr/src/php-yaf-yaf-2.2.9/tools/cg/output/app/usr/local/nginx/html/



3) A typical directory structure for YAF applications:



+ Public



|-index.php// import file



|-htaccess/ rewrite rules



|+ CSS



|+ img



|+ JS



+ conf



|-Application.ini// configuration file



+ Application



|+ Controllers



|-index.php// Default controller



|+ views



|+ index// controller



|-index.phtml// default view



|+ modules// other modules



|+ Library// local class libraries



|+ models//model Directory



|+ plugins// plugin directory






Entry file: Is the entrance to all requests, generally with the aid of the rewrite rule, all requests are redirected to the portal file, a classic entry file is as follows:



[Email protected] app]# vim index.php



<?php



Define (' Application_path ', DirName (__file__));



$application = new Yaf_application (Application_path. "/conf/application.ini");



$application->bootstrap ()->run ();



?>



Configuration file: In Yaf , the configuration file supports inheritance and supports sections. and support for PHP constants, do not worry about the configuration file is too large to cause parsing performance problems, because Yaf will load the configuration file at the first run, the contents of the format is kept in memory, Until the configuration file has been modified, it will be loaded again, a simple configuration file:



[Email protected] conf]# vim Application.ini



[Common]



Application.directory = Application_path "/application"



Application.dispatcher.catchException = TRUE



[Product:common]






Rewrite rule: Unless we use a routing protocol based on query string (Yaf_route_simple, Yaf_route_supervar), we need to use WebServer provides a Rewrite rule that puts all requests for this application , are directed to the entry file mentioned above;



Nginx Rewrite (nginx.conf), modify the file as follows:



[Email protected] conf]# vim nginx.conf



Location/{



Root Html/app;



#root html;



Index index.html index.htm index.php;



}



Location ~ \.php$ {



Root Html/app;



#root html;



Fastcgi_pass 127.0.0.1:9000;



Fastcgi_index index.php;



Fastcgi_param Script_filename/scripts$fastcgi_script_name;



Include Fastcgi_params;



}



if (!-e $request _filename) {



#rewrite ^/(. *)/index.php/$1 last; not this way .



Rewrite ^/(. *)/index.php?$1 last;



}



After modifying the configuration file nginx.conf , remember to restart:



[[Email protected] conf]# service Nginx restart



Nginx:the configuration file/usr/local/nginx/conf/nginx.conf syntax is OK



Nginx:configuration file/usr/local/nginx/conf/nginx.conf Test is successful



stopping nginx: [OK]



Starting nginx: [OK]






4) A simple example of MVC based on the YAF Framework :


controller (controllers): InYafin, the default module/Controller/action, are all based onIndexnamed, of course, this can be modified by the configuration file. For the default module, the controller's directory isApplicationunder the directoryControllersdirectory,Actionthe naming convention is"name+action ";


[Email protected] controllers]# vim test.php


<?php
class TestController extends Yaf_Controller_Abstract {
        public function testAction() {
                //1. fetch query
                $get = $this->getRequest()->getQuery("get", "default value");
                //2. fetch model
                $model = new TestModel();
                //3. assign
                $this->getView()->assign("lists", $model->selectName());
        return TRUE;
        }
}
?>


Model (models): Data Acquisition class , access to databases, files, other systems, etc.



[Email protected] models]# vim test.php


<? php
/ **
 * @models data acquisition class, can access databases, files, other systems, etc.
  * /
class TestModel {
    public function __construct () {
    }
    public function selectName () {
          $ host = "localhost";
          $ user = "root";
          $ passwd = "20082009";
          $ db = "heat";
          $ query = "select name, content from heat1 order by name";
          $ mysqli = new mysqli ($ host, $ user, $ passwd) or dir ("Unable to connect to mysql!");
          $ mysqli-> select_db ($ db);
          $ mysqli-> query ("SET NAMES 'utf8'"); //
          $ result = $ mysqli-> query ($ query);
          while ($ rows = $ result-> fetch_array ()) {
             $ lists [] = $ rows;
             // $ lists = iconv ("utf-8", "gbk", $ lists); // The encoding conversion function iconv can only accept string parameters
         }
         $ result-> free (); // Free resources
         $ mysqli-> close (); // Close the database connection
         $ result = $ lists;
         // Array encoding solution
         $ result = eval ('return' .iconv ('utf-8', 'gbk', var_export ($ result, true)). ';');
         return $ result;
      }
}
?>


Where the contents of table heat1 in database heat are:








Views:Yaf supports a simple view engine and enables users to customize their own view engine, such as Smarty .



For the default module, the path to the view file is a lowercase action in the Views directory under the application directory name in the directory;



[Email protected] test]# vim test.phtml





<? php
// loop output array contents
foreach ($ lists as $ list) {
echo $ list ['name'], "please see:", $ list ['content'];
echo "<br />";
}
?> 





In the browser input:http://172.16.2.33/index.php/Test/test, the following page is displayed:





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Install the YAF framework in LNMP (linux+nginx+mysql+php) environment and write an MVC example


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.