作为程序员一定要保持良好的睡眠,才能好编程

yaf第一课 php支持yaf扩展

发布时间:2018-05-27

php默认是不支持yaf的,


推荐一个官方的扩展包   pecl.php.net  这个网站


然后在搜索框中输入:yaf




http://pecl.php.net/package/yaf




就可以看到yaf的的扩展包,我们选择一个stable version的包。 如果是windows 按照需要进行下载。

windows 分64位 和 x86   还分 nt 和 nts  在phpinfo() 这个方法中就能看到。

截图02.png




截图03.png


这样windows就支持yaf了。



Linux支持yaf  PHP


yaf-master.zip 或者直接下载这个安装包


如果是linux的话,需要下载 tar.gz 的包, 使用 tar -xf 进行解压  然后运行 phpize  进行编译 

 wget 

http://pecl.php.net/get/yaf-3.0.7.tgz


进行解压文件 如果下载的是zip 文件,使用 unzip yaf-master.zip 进行解压


如果是tar.gz  使用  tar -xzf yaf-3.0.7.tgz


进入yaf的目录后,使用 phpize  生成 configure 文件

/usr/local/php/php5.6/bin/phpize  




当运行成功后,此目录下会产生一个  configure 的编译文件


然后 运行 

./configure 


或:

./configure --with-php-config=/usr/local/php/php5.6/bin/php-config



make && make install 


进行编译安装 。



当编译安装成功以后,就会产生一个  yaf.so  这就是php的一个扩展。

生成的so文件在这个目录下:

/usr/local/php/php7/lib/php/extensions/no-debug-non-zts-20170718/

yaf.png

find ./ -name 'yaf.so'



如果不知道php.ini 文件在什么位置,可以使用 phpinfo(); 打印出来查看

然后打开php.ini  把 yaf.so 引入进来。

phpini yaf.png


重新启动php 即可。


service phpfpm7 restart   phpfpm进行重新启动


phpinfoyaf.png

看到上面的配置,证明php 已经可以使用yaf了。

=================================




安装 yaf

https://github.com/laruence/yaf




下载这个安装包  


yaf-master.zip

进行解压  然后进入 tools/cg 目录文件夹  使用  yaf_cg  进行生成目录。



./yaf_cg  web


查看一下目录 cd output 目录


tree web


yaf_cg.png


代码执行成功后,这是一个最基本、最简单的项目。


下面是服务器,需要进行配置的代码,选择自己适用的,配置好,重新启动服务器:


Rewrite rules


Apache


#.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php


Nginx


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;
  }
}


Lighttpd


$HTTP["host"] =~ "(www.)?domain.com$" {
  url.rewrite = (
     "^/(.+)/?$"  => "/index.php/$1",
  )
}


我本地使用的是 nginx  那么来配置一个nginx 来试试看:


server {
        listen       8059;
        client_max_body_size 100m;
        server_name localhost;
        index index.php index.html;
        root  /usr/local/nginx/html/api;
        #error_page 404 = /404/index.html;
        location / {
            root   /usr/local/nginx/html/api;
            index  index.php;
        }
        if (!-e $request_filename) {
         rewrite ^/(.*)  /index.php/$1 last;
        }

        location ~ .*\.(jpg|jpeg|png|gif|js|css)$ {
                expires 1d;
        }
        location ~ \.php(/|$) {
                fastcgi_pass   127.0.0.1:9007;
                fastcgi_index  index.php;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }
}


yafnginx.png


运行结果:

yafrun.png


到这里,最简单的yaf框架,已经运行起来了。


api.zip


可以下载这个最简单的yaf 框架进行扩展业务。