官术网_书友最值得收藏!

  • Yii Project Blueprints
  • Charles R. Portwood II
  • 406字
  • 2021-08-05 17:50:35

Initializing the project

With our final database structure figured out, we can now start writing code. Using the instructions in the official guide ( Yii is installed, navigate to your webroot directory, and create a new folder called tasks. Next, navigate inside the tasks folder, and create the following folder structure to serve as our application's skeleton:

tasks/
    assets/
    protected/
              commands/
              components/
              config/
              controllers/
              data/
              migrations/
              models/
              runtime/
              views/
                    layouts/
                    projects/
                    tasks/
                    site/

Tip

Yii has a built-in tool called yiic, which can automatically generate a skeleton project. Refer to the quick start guide (http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app) for more details.

Depending upon the web server you are using, you may also need to create a .htaccess file in the root directory of your tasks folder. Information about how to set up your application for the web server you are using can be found in the quick start guide (http://www.yiiframework.com/doc/guide/1.1/en/quickstart.apache-nginx-config).

After setting up our skeleton structure, we can first create our configuration file located at protected/config/main.php. Our configuration file is one of the most important files of our application as it provides Yii with all the critical information necessary to load and configure our application. The configuration file informs Yii about the files to be preloaded by Yii's built-in autoloader, the modules to be loaded, the component to be registered, and any other configuration options we want to pass to our application.

For this application, we will be enabling the Gii module, which will allow us to create models based upon our database structure. We will also enable two components, urlManager and db, which will allow us to set up custom routes and access our SQLite database. Have a look at the following code snippet:

<?php
return array(
   'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
   'name'=>'Task Application',
   'import'=>array(
        'application.models.*',
        'application.components.*',
    ),
    'modules'=>array(
        // Include the Gii Module so that we can
//generate models and controllers for our application
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>false,
            'ipFilters'=>false
        ),
    ),
    'components'=>array(
        'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName'=>false,
            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),
        // Define where our SQLite database is going to be
        // stored, relative to our config file
        'db'=>array(
            'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/tasks.db',
        )
    )
);

Next, we can create our index.php file as follows, which will serve as our bootstrap endpoint for our web application:

<?php
// change the following paths if necessary
$yii='/opt/frameworks/php/yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';

// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

require_once($yii);
Yii::createWebApplication($config)->run();

Finally, we can create our applications yiic file in protected/yiic.php as follows, which will allow us to run console commands native to Yii from our application:

<?php
// change the following paths if necessary
$config=dirname(__FILE__).'/config/main.php';
$config = require($config);
require_once('/opt/frameworks/php/yii/framework/yiic.php');
主站蜘蛛池模板: 微山县| 吐鲁番市| 乡城县| 萨迦县| 府谷县| 霞浦县| 平谷区| 晴隆县| 承德市| 乐平市| 开远市| 兰溪市| 司法| 二连浩特市| 沁源县| 利川市| 佛坪县| 城市| 南郑县| 体育| 南平市| 东阳市| 井冈山市| 烟台市| 九台市| 南京市| 白水县| 林甸县| 乌恰县| 龙陵县| 广昌县| 常熟市| 横山县| 鄱阳县| 东丽区| 六盘水市| 邵东县| 齐齐哈尔市| 神池县| 云南省| 邮箱|