Yaf framework installation guide

不言
Release: 2023-03-25 18:28:02
Original
11654 people have browsed it

This article mainly introduces the Yaf framework installation guide, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

Speaking of the PHP framework, many people The impression remains on a code package based on MVC's various functional combinations implemented by PHP. Very few people know that C language can also write PHP frameworks, and the speed is more than 10 times faster than frameworks written in PHP.

Yaf is a PHP framework written in C language. It runs the framework as a PHP extension. Only the core functions of MVC are implemented: routing and MVC. The Yaf kernel is streamlined and stable enough, so there are almost no operational problems. Risks are controllable and performance is excellent. Of course, because it is simple, you need to implement operations such as DB closure and Session expansion.

As a PHP programmer, I should be familiar with the framework written by Asia’s number one programmer: Brother Niao, so what are its advantages?

  • The PHP framework developed in C language brings almost no additional performance overhead compared to native PHP.

  • All framework classes do not need to be compiled. They are loaded when PHP starts and are resident in memory.

  • Shorter memory turnover cycle, improves memory utilization and reduces memory usage Occupancy rate.

  • Flexible automatic loading. Supports both global and local loading rules to facilitate class library sharing.

  • High-performance view Engine.

  • A highly flexible and extensible framework that supports custom view engines, plug-ins, custom routing, etc.

  • Build a variety of routes that are compatible with various currently common routing protocols.

  • Powerful and highly flexible configuration file support. It also supports caching configuration files to avoid complex configuration structures. Performance loss.

  • In the framework itself, dangerous operating habits are prohibited.

  • Faster execution speed, less Memory usage.

Framework installation:

1:Framework installation

Yaf extension homepage:http://pecl. php.net/package/yaf

$ wget http://pecl.php.net/get/yaf-3.0.7.tgz
$ tar -zxvf yaf-3.0.7.tgz
$ cd yaf-3.0.7
$ /path/to/phpize
$./configure --with-php-config=/path/to/php-config
$ make && make test && make install
Copy after login

tip:

When executing the compilation command /path/to/phpize, the following error may occur:

Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
Copy after login

You can refer to: https://blog.csdn.net/alen_xiaoxin/article/details/80255766 to find solutions.

After compiling and generating the extension, modify php.ini and add the following configuration at the end of the php.ini file:

[yaf]
yaf.use_namespace = 0
yaf.environ = 'product'
yaf.cache_config = 0
yaf.name_suffix = 1
yaf.lowcase_path = 1

extension = yaf.so
Copy after login

After adding it, you can check whether there is a yaf extension in phpinfo.

Configuration instructions:

  • yaf.user_namespace is 1 to enable namespace mode. 0 off.

  • yaf.environ is the environment configuration read by Yaf by default.

  • yaf.cache_config Whether to cache project configuration.

  • yaf.name_suffix Enable suffix. After it is 1, the class name will be loaded in XxxModel.php, XxxController.php mode.

  • yaf.lowcase_path The directory part in the path information will be converted to lowercase.

2. Create the first Yaf project

There are two ways to create it:


  1. Create the directory manually

  2. Use the command line provided by Yaf to generate the directory

第二种方法可自行到:https://github.com/laruence/php-yaf,下载源码,因为 Yaf 提供的命令工具没有随 Yaf 源码一起,在该项目下面有一个tools文件夹,里面就是命令行工具。

一个典型的目录结构:

<p style="margin-bottom: 7px;">+ public<br/>  |- index.php //入口文件<br/>  |- .htaccess //重写规则    <br/>  |+ css<br/>  |+ img<br/>  |+ js<br/>+ conf<br/>  |- application.ini //配置文件   <br/>+ application<br/>  |+ controllers<br/>     |- Index.php //默认控制器<br/>  |+ views    <br/>     |+ index   //控制器<br/>        |- index.phtml //默认视图<br/>  |+ modules //其他模块<br/>  |+ library //本地类库<br/>  |+ models  //model目录<br/>  |+ plugins //插件目录<br/></p>
Copy after login

入口文件:

入口文件是所有请求的入口, 一般都借助于rewrite规则, 把所有的请求都重定向到这个入口文件。

一个经典的入口文件piublic/index.php:

<?php
define("APP_PATH",  realpath(dirname(__FILE__) . &#39;/../&#39;)); /* 指向public的上一级 */
$app  = new Yaf_Application(APP_PATH . "/conf/application.ini");
$app->run();
Copy after login

重写规则:

Apache的Rewrite (httpd.conf):

#.htaccess, 当然也可以写在httpd.conf
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
Copy after login

Nginx的Rewrite (nginx.conf):

server {
  listen ****;
  server_name  domain.com;
  root   document_root;
  index  index.php index.html index.htm;

  if (!-e $request_filename) {
    rewrite ^/(.*)  /index.php/$1 last;
  }
}
Copy after login

配置文件:

在Yaf中, 配置文件支持继承, 支持分节. 并对PHP的常量进行支持. 你不用担心配置文件太大造成解析性能问题, 因为Yaf会在第一个运行的时候载入配置文件, 把格式化后的内容保持在内存中. 直到配置文件有了修改, 才会再次载入。

一个简单的配置文件application/application.ini:

[product]
;支持直接写PHP中的已定义常量
application.directory=APP_PATH "/application/"
Copy after login

控制器:

在Yaf中, 默认的模块/控制器/动作, 都是以Index命名的, 当然,这是可通过配置文件修改的.

对于默认模块, 控制器的目录是在application目录下的controllers目录下, Action的命名规则是"名字+Action"

默认控制器application/controllers/Index.php

<?php
class IndexController extends Yaf_Controller_Abstract {
   public function indexAction() {//默认Action
       $this->getView()->assign("content", "Hello World");
   }
}
?>
Copy after login

视图文件:

Yaf支持简单的视图引擎, 并且支持用户自定义自己的视图引擎, 比如Smarty。

对于默认模块, 视图文件的路径是在application目录下的views目录中以小写的action名的目录中。

一个默认Action的视图application/views/index/index.phtml

<html>
 <head>
   <title>Hello World</title>
 </head>
 <body>
  <?php echo $content;?>
 </body>
</html>
Copy after login

运行:

在浏览器输入你服务器配置的域名即可:

http://www.yourhostname.com/application/index.php
Copy after login

我是在本地配置的直接把端口指向public目录,所以直接输入:localhost:8081,即可看到

注意:

如果看不到Hello world,那么请到服务器查看PHP的错误日志,找出问题在哪里。

The above is the detailed content of Yaf framework installation guide. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!