美文网首页
创建 yaf 项目目录及文件(windows)

创建 yaf 项目目录及文件(windows)

作者: Hyso | 来源:发表于2019-04-11 00:27 被阅读0次

    yaf 项目目录结构

    + public
      |- index.php // 入口文件
      |- .htaccess // 重写规则
      |- composer.json // composer 配置文件
      |+ static
         |- css
         |- img
         |- js
    + conf
      |- application.ini // 项目配置文件   
    + application
      |+ controllers
         |- Index.php // 默认控制器
      |+ views    
         |+ index   // 控制器
            |- index.phtml // 默认视图
      |+ modules // 其他模块
      |+ library // 本地类库
      |+ models  // model目录
      |+ plugins // 插件目录
    

    入口文件:public/index.php

    <?php
    // 定义项目根目录:public 目录的上一级目录
    define("APP_PATH",  realpath(dirname(__FILE__).'/../'));
    
    $app = new Yaf_Application(APP_PATH."/conf/application.ini");
    $app->bootstrap()->run();
    

    apache 重写规则文件:public/.htaccess

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

    apache 虚拟主机配置

    • 项目根目录路径为:E:/Project/hyso/yaf,Directory 配置项为项目根目录下的 public 目录(index.php 文件所在目录)
    • 虚拟主机域名:local-yaf.bmtrip.com,本地 hosts 文件相应加上此域名的配置
    <VirtualHost *:8080>
        ServerAdmin tobehyso@163.com
        DocumentRoot "E:/Project/hyso/yaf/public"
        ServerName local-yaf.bmtrip.com
        <Directory "E:/Project/hyso/yaf/public">  
            Options FollowSymLinks Includes ExecCGI
            AllowOverride All 
            Require all granted  
        </Directory>  
        ErrorLog "logs/local-yaf.bmtrip.com-error.log"
        CustomLog "logs/local-yaf.bmtrip.com-access.log" common
    </VirtualHost>
    

    composer 配置文件:public/composer.json

    {
        "config": {
            "vendor-dir": "../vendor"
        }
    }
    

    处理 composer 依赖关系

    运行命令行窗口,切换到项目根目录,执行以下命令:

    composer install
    

    默认控制器文件:application/controllers/Index.php

    <?php
    class IndexController extends Yaf_Controller_Abstract
    {
       /**
        * 默认Action
        * 
        */
       public function indexAction()
       {
           $this->getView()->assign("content", "Hello World");
       }
    
       /**
        * test Action
        * 
        */
       public function testAction()
       {
           $this->getView()->assign("content", "test");
    
           $this->getView()->display('index/index.phtml');
    
           return false;
       }
    }
    

    项目配置文件:conf/application.ini

    [common]
    application.directory = APP_PATH "/application/" 
    application.bootstrap = APP_PATH "/application/Bootstrap.php" 
    
    [product:common]
    

    Bootstrap 文件:application/Bootstrap.php

    <?php
    class Bootstrap extends Yaf_Bootstrap_Abstract
    {
    
        public function _initConfig()
        {
            $config = Yaf_Application::app()->getConfig();
    
            Yaf_Registry::set("config", $config);
        }
    
        public function _initDefaultUrl(Yaf_Dispatcher $dispatcher)
        {
            $dispatcher
            ->setDefaultModule("Index")
            ->setDefaultController("Index")
            ->setDefaultAction("test");
        }
    
        public function _initAutoload()
        {
            require '../vendor/autoload.php';
        }
    }
    

    默认视图文件:application/views/index/index.phtml

    • 视图文件目录:application/views
    • 默认控制器视图文件目录(目录名与控制器文件名保持一致):application/views/index
    <html>
        <head>
            <title>Hello World</title>
        </head>
        <body>
            <?php echo $content;?>
        </body>
    </html>
    

    浏览器中访问控制器

    相关文章

      网友评论

          本文标题:创建 yaf 项目目录及文件(windows)

          本文链接:https://www.haomeiwen.com/subject/hivaiqtx.html