Home Backend Development PHP Tutorial THINKPHP controller_PHP tutorial

THINKPHP controller_PHP tutorial

Jul 14, 2016 am 10:12 AM
action lib thinkphp exist how controller New


How to add a new controller?


Add a new controller in Lib/Action: ProductAction.class.php:


class ProductAction extends Action{
 
Public function index(){
echo 'product module, index method';
}
}


Then access via url: This introduces a very important concept:

URL scheduling mode: we use different UR access methods when accessing the website


tp supports four access methods: they can be controlled through the URL_MODEL parameter.


1. Ordinary mock test: supported by default


www.tp.com/index.php?m=Product&a=index&id=1

m module name

a action


2.pathinfo module (default scheduling mode in tp):

http://www.tp.com/index.php/Product/index/id/1/name/zhangsan

product module name

index method name

id is a parameter

1 is the value. . . . . Generally, the following parameters appear in pairs. Separated by /.


3. rewrite mode, that is, rewriting.

used in page staticization

You can omit the entry file:

http://www.tp.com/Product/index/id/1/name/zhangsan

Setting method:

1. The mod_rewrite.so module is loaded in the httpd.conf configuration file

2. Change AllowOverride None from None to All (note that it cannot appear at the same time as #Options Indexes, otherwise there will be no permission to access)


3. Make sure URL_MODEL is set to 2

4. Save the following content as a .htaccess file and place it in the same directory as the entry file

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]


4.Compatibility mode:

If the web server does not support pathinfo or rewrite mode, but we still want to use it, we can consider using compatibility mode


http://www.tp.com/index.php?s=Product/index/id/1/name/zhangsan

Or: http://www.tp.com/?s=Product/index/id/1/name/zhangsan


In the configuration file, 0123 of a URL_MODEL are represented respectively. It’s supported by default. What’s going on? ? ? ?


No matter what mode, normal mode is supported, and the difference can be seen when the form is submitted.

Path separator: 'URL_PATHINFO_DEPR' => '-', // In PATHINFO mode, the separation symbol between each parameter


http://www.tp.com/?s=Product-index-id-1-name-zhangsan

Air controls in the controller? In this way, you can simplify the URL and take advantage of one of its characteristics.

The system cannot find the specified method and the method to be executed.


Add the following method to the controller:

/*
* $name represents the requested method
*/
Public function _empty($name){
echo 'The requested page cannot be displayed'.$name;
}


5. Empty module

The concept of empty module means that when the system cannot find the specified module name, the system will try to locate the empty module (EmptyAction). We can use this mechanism to customize error pages and optimize URLs.

l EmptyAction

MODULE_NAME

We now try to request a url

http://localhost/tp/index.php/Student/shanghai

Since there is no StudentAction controller in our system, an error will be reported

We create a class called EmptyAction in the project. In the future, if the system cannot find the corresponding module, it will automatically locate this Action. If we add another method called _empty to this class, it will be the same. Block all errors from url


5. Project grouping

In large-scale projects, a large project is often composed of several small projects. For example: It may be caused by

Front-end projects, back-end projects, member blogs, forums

l config.php

l APP_GROUP_LIST Group list

l DEFAULT_GROUP                                                                                             

In our project, start grouping now:

1) Front desk project Home

2) Backstage project Admin

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477233.htmlTechArticleHow to add a new controller? Add a new controller in Lib/Action: ProductAction.class.php: ?php class ProductAction extends Action{ public function index(){ echo product module, ind...
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 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 run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

The first choice for CS players: recommended computer configuration The first choice for CS players: recommended computer configuration Jan 02, 2024 pm 04:26 PM

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 Black DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 Black Sep 04, 2024 am 06:51 AM

DJI has not confirmed any plans to introduce a new action camera yet. Instead, it seems that GoPro will get ahead of its rival this year, having teased that it will introduce two new action cameras on September 4. For context, these are expected to a

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Learning Laravel from scratch: Detailed explanation of controller method invocation Learning Laravel from scratch: Detailed explanation of controller method invocation Mar 10, 2024 pm 05:03 PM

Learning Laravel from scratch: Detailed explanation of controller method invocation In the development of Laravel, controller is a very important concept. The controller serves as a bridge between the model and the view, responsible for processing requests from routes and returning corresponding data to the view for display. Methods in controllers can be called by routes. This article will introduce in detail how to write and call methods in controllers, and will provide specific code examples. First, we need to create a controller. You can use the Artisan command line tool to create

See all articles