Yii入门教程之目录结构、入口文件及路由设置_php实例
项目名称从“FantaCMS”修改成为“testyii”—————— 俗!
1,项目目录结构分析
2,Yii入口文件分析
在Yii启动项目时,传递了项目主配置文件数组,那么Yii会在整个应用中绑定一个全局的Yii对象并且可以通过如下方法来调用该对象:Yii::app()
Yii系统把配置文件中的数组值,通过键值对的形式绑定到了该对象上,例如在配置文件中我们配置了:
'name'=>'My Web Application',
那么我们在项目的任何一个地方通过该对象的属性就可以获取到“name”的值,方法为:
Yii::app()->name;
3,Yii路由设置
同时应该知道:对于控制器名,Yii首先会检测当前的控制器名是否是一个“模块名”,如果是模块名则先定位到模块。
“模块”会在后面搭建项目的时候解释。
通过上面的路由访问到的就是:SiteController.php类文件下的SiteController类中的actionIndex方法
控制器文件位于:protected/controllers 目录下,也就是我们的控制器文件存放目录
注意Yii中控制器文件和动作方法名的书写方式,控制器有统一的后缀”Controller“,动作方法也有统一的前缀”action“,同时要求动作方法名的命名规范符合”除第一个单词外其它每个单词的首字母要大写“
由于Yii默认的控制器名为:site
默认的动作名为:index
因此上述通过指定控制器名动作名访问的路径和直接访问:http://localhost/testyii/ 的效果是一致的
4,视图调用
在动作方法中,调用:$this->render('index');
来为对应的动作方法指定视图文件,视图文件位于:protected/views/site 目录下
其中:site为对应的控制器名文件夹,每个控制器名在视图中都应该具有一个唯一的文件夹名与其相对应
然后在动作方法中通过‘index'来指定显示的具体视图文件是site控制器下的指定的‘index.php'视图文件
另外还需要注意:
调用视图的方法有两个:
$this->render ----> 会调用模板文件
和
$this->renderPartial -----> 不会调用模板文件
它们之间的区别,也如上所述。
5,视图模板设置
打开SiteController.php文件,其中的代码截图如下:
我们发现:Yii应用中,每个控制器都要继承自公共控制器“Controller”
然后打开“Controller”控制器文件:Controller.php,它位于:protected/components 目录下
“Controller”控制器代码截图如下:
Yii通过:public $layout='//layouts/column1'; 来指定动作方法的公共模板文件
公共模板文件位于:protected/views/layouts 目录下,如下图:
现在我们来创建我们的模板文件:testlayout.php,代码如下:
其中”“为Yii中规定的模板文件中的内容替换方法
然后,修改 ”Controller“控制器中的模板文件为:public $layout='//layouts/testlayout';
然后访问:http://localhost/testyii/index.php?r=site/index 结果如图:
然后我们发现,模板文件已经变成我们自己指定的了,然后假如你不需要视图文件渲染模板文件,那么你可以在动作方法中调用视图文件的时候使用:$this->renderPartial 方法
或者你整个项目都不需要调用模板文件,那么你可以在动作方法中调用视图文件时全部用:$this->renderPartial
也或者将视图模板文件设置为”空“,例如:public $layout='';
下一节继续:Yii的魔术师:gii,Yii模块及模块自定义

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the increasing number of web applications, web development frameworks have become an important part of modern web application development. Today we are going to introduce a popular web framework - Flight, and how to set up routing in Flight. Flight is a minimalist web framework optimized for small web applications and JSON API. It is characterized by being lightweight, easy to learn and use, and has no cumbersome configuration files. It provides basic routing functionality to make your code

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

Yii framework: This article introduces Yii's method of converting objects into arrays or directly outputting them into json format. It has certain reference value and I hope it can help you.

If you're asking "What is Yii?" check out my previous tutorial: Introduction to the Yii Framework, which reviews the benefits of Yii and outlines what's new in Yii 2.0, released in October 2014. Hmm> In this Programming with Yii2 series, I will guide readers in using the Yii2PHP framework. In today's tutorial, I will share with you how to leverage Yii's console functionality to run cron jobs. In the past, I've used wget - a web-accessible URL - in a cron job to run my background tasks. This raises security concerns and has some performance issues. While I discussed some ways to mitigate the risk in our Security for Startup series, I had hoped to transition to console-driven commands
