Home Backend Development PHP Tutorial Yii入门教程之目录结构、入口文件及路由设置_php实例

Yii入门教程之目录结构、入口文件及路由设置_php实例

Jun 07, 2016 pm 05:15 PM
yii Directory Structure Routing settings

项目名称从“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模块及模块自定义

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up routing in Flight framework? How to set up routing in Flight framework? Jun 03, 2023 am 09:01 AM

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

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

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.

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

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

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

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

Symfony vs Yii2: Which framework is better for developing large-scale web applications? Symfony vs Yii2: Which framework is better for developing large-scale web applications? Jun 19, 2023 am 10:57 AM

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

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

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

How to convert yii objects into arrays or directly output to json format How to convert yii objects into arrays or directly output to json format Jan 08, 2021 am 10:13 AM

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.

Yii2 Programming Guide: How to run Cron service Yii2 Programming Guide: How to run Cron service Sep 01, 2023 pm 11:21 PM

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

See all articles