Yii

来源:互联网 发布:电脑c盘数据恢复 编辑:程序博客网 时间:2024/06/02 15:45

Yii 的工作模式 :
这里写图片描述

假设您已经安装了 Yii 的应用环境

入口文件 index.php

<?php//开启调试模式defined('YII_DEBUG') or define('YII_DEBUG', true);  //线上环境设为falsedefined('YII_ENV') or define('YII_ENV', 'dev');     //线上环境设为pro//composer 载入require(__DIR__ . '/../vendor/autoload.php');//Yii 核心框架文件require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');//载入应用主体配置$config = require(__DIR__ . '/../config/web.php');//创建应用主体 所有的应用从这里开始(new yii\web\Application($config))->run();

简单示例 路径 controllers\home\IndexController;

文件名 IndexController.php

<?phpnamespace app\controllers\home;use yii\web\Controller;class IndexController extends Controller{    public function actionIndex()    {        echo "hello world!";    }}//访问 url : http://host/index.php?r=home/index/index//请确保index.php 项目根目录环境下面?>

显示视图 路径 controllers\home\CustomController;

<?phpnamespace app\controllers\home;use Yii;use yii\web\Controller;class CustomController extends Controller{    //访问url : http://host/index.php?r=home/custom/index-view&age=22&name=谭勇    public function actionIndexView($name='',$age=0)    {        $model = array(            'name'=>$name,            'age' =>$age        );        return $this->render('index',$model);        //注意这里的url路由机制 其中model 是要传递给视图的数据    }    public function actionIndexView1()    {        $model = Yii::$app->request->get();//获得get请求 param 数据        return $this->render('index',$model);        //注意这里的url路由机制 其中model 是要传递给视图的数据    }}?>

路径 : views/home/custom/index.php

<?php    var_dump($model);?>
原创粉丝点击