How to access the controller in yii2
Yii2 default controller has only one directory under the controller directory. When needed for development, you need to add a new directory under this directory, that is, under Add a secondary directory under the controller, such as controller/api/MemberController.php, and directly access /api/member/. (Recommended learning: yii tutorial)
This aspect of management and code modularization is neat.
For example, you need to create a new api directory under the default controllers directory. At this time, the api-related controllers are placed in the api.
The created directory structure is as shown below:
The code of MemberController.php is as follows:
<?php //注意此处的命名空间有\api (关键关键关键) namespace app\controllers\api; use Yii; use yii\web\Controller; //位于API目录下面的Member控制器 class MemberController extends Controller{ // public function actionIndex(){ echo 'This is member index'; exit(); } public function actionTest(){ echo 'TTTTTTTTTTTTTTT'; exit(); } public function actionHello(){ return 'Hello World!!!'; exit(); } }
For the corresponding view layer that needs to be rendered, please also create the corresponding view layer under views.
There is another way to use modules. For example, in the background admin part, you can create a module to facilitate integrated management.
The same is true in yii1. It’s just that there are more namespaces in yii2. Need to be pointed out in particular (see code).
Added screenshots of my own experiments
The above is the detailed content of How to access the controller in yii2. For more information, please follow other related articles on the PHP Chinese website!

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



The article discusses best practices for deploying Yii applications in cloud-native environments, focusing on scalability, reliability, and efficiency through containerization, orchestration, and security measures.

The article discusses key considerations for using Yii in serverless architectures, focusing on statelessness, cold starts, function size, database interactions, security, and monitoring. It also covers optimization strategies and potential integrati

Yii's built-in testing framework enhances application testing with features like PHPUnit integration, fixture management, and support for various test types, improving code quality and development practices.

The article discusses strategies for testing Yii applications using Codeception, focusing on using built-in modules, BDD, different test types, mocking, CI integration, and code coverage.

The article discusses implementing real-time data synchronization using Yii and WebSockets, covering setup, integration, and best practices for performance and security.

The article discusses tools for monitoring and profiling Yii application performance, including Yii Debug Toolbar, Blackfire, New Relic, Xdebug, and APM solutions like Datadog and Dynatrace.

The article discusses Yii's benefits for SaaS development, focusing on performance, security, and rapid development features to enhance scalability and reduce time-to-market.

The article discusses key considerations for deploying Yii applications in production, focusing on environment setup, configuration management, performance optimization, security, logging, monitoring, deployment strategies, and backup/recovery plans.
