基於 YAF 的實際案例:鉤子、事件、模塊、插件、多模板、多語言、SQL 集中式管理...
支援電子商務平台、OA、ERP、IaaS、PaaS、SaaS、部落格、CMS...
任何平台所需的共同功能:使用者、ACL、選單...
https://github.com/letwang/HookPHP
範例 #1 典型的應用程式目錄佈局
- index.php - .htaccess + conf |- application.ini //application config - application/ - Bootstrap.php + controllers - Index.php //default controller + views |+ index - index.phtml //view template for default action + modules - library - models - plugins
範例 #2 程式入口
頂層目錄中的 index.php 是應用程式的唯一入口,您應該將所有請求都重寫到它。(您可以在 Apache + php_mod 中使用 .htaccess)
<?php
define("APPLICATION_PATH", dirname(__FILE__));
$app = new Yaf_Application(APPLICATION_PATH . "/conf/application.ini");
$app->bootstrap() //呼叫 Bootstrap.php 中定義的 bootstrap 方法
->run();
?>
範例 #3 重寫規則
#for apache (.htaccess) RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* index.php #for 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; } } #for lighttpd $HTTP["host"] =~ "(www.)?domain.com$" { url.rewrite = ( "^/(.+)/?$" => "/index.php/$1", ) }
範例 #4 應用程式設定
[yaf] ;APPLICATION_PATH is the constant defined in index.php application.directory=APPLICATION_PATH "/application/" ;product section inherit from yaf section [product:yaf] foo=bar
範例 #5 預設控制器
<?php
class IndexController extends Yaf_Controller_Abstract {
/* 預設動作 */
public function indexAction() {
$this->_view->word = "hello world";
//或
// $this->getView()->word = "hello world";
}
}
?>
範例 #6 預設視圖範本
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php echo $word;?>
</body>
</html>
範例 #7 執行應用程式
上述範例將輸出類似以下的內容
<html> <head> <title>Hello World</title> </head> <body> hello world </body> </html>
注意事項:
您也可以使用 Yaf 程式碼產生器產生上述範例,可以在 yaf@github 找到它。
基於 YAF 的實際案例:鉤子、事件、模塊、插件、多模板、多語言、SQL 集中式管理...
支援電子商務平台、OA、ERP、IaaS、PaaS、SaaS、部落格、CMS...
任何平台所需的共同功能:使用者、ACL、選單...
https://github.com/letwang/HookPHP
替代方案
您可以使用 Yaf 程式碼產生器產生上述範例:https://github.com/laruence/php-yaf/tree/master/tools/cg
PHP8 Yaf3.3.3 按照官網配置導致 Nginx 死循環:
rewrite or internal redirection cycle while processing "/index.phpindex.phpindex.......
解決方案:
範例 #3 重寫規則
#for 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;
}
}
唯一的變化,是多出一個?問號
nginx 重寫不知為什麼死循環
rewrite ^/(.*) /index.php/$1 last;
/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/Index
後來我改成查詢字串就解決了。
location / {
index index.php index.html index.htm;
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?_path_info=/$1 last;
}
}
在 index.php 中,把查詢字串 "_path_info" 放到 $_SERVER 變數中。
<?php
ini_set("display_errors",true);
error_reporting(E_ALL|E_ERROR);
//print_r($_SERVER);
$_SERVER['PATH_INFO']=@$_GET['_path_info']; //加上這一行,Yaf 只認 PATH_INFO
define("APPLICATION_PATH",dirname(__DIR__));
$app = new Yaf\Application(APPLICATION_PATH.'/conf/application.ini');
$app->bootstrap()->run();
?>
我在 "application" 目錄設定成功如下:
- application/
- Bootstrap.php
+ modules
+ Index
+ controllers
- Index.php //預設控制器
+ views
|+ index
- index.phtml //預設動作的視圖模板
- library
- models
- plugins
Bootstrap.php 至少要包含以下三行:
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract {
}
nginx 的 rewrite 規則應該是:
if (!-e $request_filename) {
rewrite ^/(.*) /index.php?$1 last;
}
http://us3.php.net/manual/zh/yaf.tutorials.php
遺失的預設 Bootstrap.php
<?php
/* bootstrap 類別應該定義在 ./application/Bootstrap.php 底下 */
class Bootstrap extends Yaf_Bootstrap_Abstract {
public function _initConfig(Yaf_Dispatcher $dispatcher) {
var_dump(__METHOD__);
}
public function _initPlugin(Yaf_Dispatcher $dispatcher) {
var_dump(__METHOD__);
}
}
使用 nginx 和 php-fpm,你可以這樣設定:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
nginx 重寫不知為什麼死循環
rewrite ^/(.*) /index.php/$1 last;
/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/Index
後來我改成查詢字串就解決了。
location / {
index index.php index.html index.htm;
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?_path_info=/$1 last;
}
}
在 index.php 中,把查詢字串 "_path_info" 放到 $_SERVER 變數中。
<?php
ini_set("display_errors",true);
error_reporting(E_ALL|E_ERROR);
//print_r($_SERVER);
$_SERVER['PATH_INFO']=@$_GET['_path_info']; //加上這一行,Yaf 只認 PATH_INFO
define("APPLICATION_PATH",dirname(__DIR__));
$app = new Yaf\Application(APPLICATION_PATH.'/conf/application.ini');
$app->bootstrap()->run();
?>