laruence / yaf

Fast php framework written in c, built in php extension
http://pecl.php.net/package/yaf
Other
4.52k stars 1.38k forks source link

yaf多模块问题 #322

Open asdfg365 opened 7 years ago

asdfg365 commented 7 years ago

1,yaf多模块,怎么配置多域名? 2,能在配置文件中,或bootstrap中修改默认模块吗?

wenjun1055 commented 7 years ago

你可以在插件plugin中来根据不同域名指定不同的Module

asdfg365 commented 7 years ago

1,在plugins 文件夹下新建Admin.php class AdminPlugin extends Yaf_Plugin_Abstract {

public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
    $request->setModuleName('Admin');
}

}

2.BootStrap.php引导文件里 public function _initPlugin(Yaf_Dispatcher $dispatcher) { $admin = new AdminPlugin(); $dispatcher->registerPlugin($admin); }

3,在public/index.php define("APP_PATH", realpath(dirname(FILE) . '/../')); $app = new Yaf_Application(APP_PATH . "/conf/application.ini"); $app->bootstrap()->run();

这样三步就可以实现修改默认模块了,感觉这样不太灵活 @laruence 还有其他实现方法吗

qloog commented 7 years ago

在Bootstrap.php里加入以下代码即可:

/**
     * 域名检测
     *
     * @param Dispatcher $dispatcher
     */
    public function _initDomainCheck(Dispatcher $dispatcher)
    {
       // admin_domain 为application.ini配置的后台域名
        if($_SERVER['HTTP_HOST'] == $this->config['admin_domain']) {
            $dispatcher->getRequest()->setModuleName('Admin');
        }

       // 同理 api_domain 为application.ini配置的api域名
        if($_SERVER['HTTP_HOST'] == $this->config['api_domain']) {
            $dispatcher->getRequest()->setModuleName('Api');
        }

    }

PS: 注意放置顺序。

lagolas commented 6 years ago

$dispatcher->getRequest()->setModuleName('Api'); 楼上赞

letwang commented 5 years ago

yaf多模块,怎么配置多域名 https://github.com/letwang/HookPHP/blob/master/README.md https://github.com/letwang/HookPHP/tree/master/app

server {
    listen 80;
    root /home/letwang/workspace/HookPHP/public/admin/;
    index index.html index.htm index.php;
    error_log /var/log/nginx/www.admin.com-error.log error;access_log /var/log/nginx/www.admin.com-access.log combined;
    server_name www.admin.com;

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

    location ~ \.php$ {
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
     }
}

server {
    listen 80;
    root /home/letwang/workspace/HookPHP/public/erp/;
    index index.html index.htm index.php;
    error_log /var/log/nginx/www.erp.com-error.log error;access_log /var/log/nginx/www.erp.com-access.log combined;
    server_name www.erp.com;

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

    location ~ \.php$ {
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
     }
}

server {
    listen 80;
    root /home/letwang/workspace/HookPHP/public/paas/;
    index index.html index.htm index.php;
    error_log /var/log/nginx/www.paas.com-error.log error;access_log /var/log/nginx/www.paas.com-access.log combined;
    server_name www.paas.com;

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

    location ~ \.php$ {
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
     }
}