THINKPHP controller _ PHP Tutorial

Source: Internet
Author: User
THINKPHP controller. How to add a controller? Add a new controller in LibAction: ProductAction. class. php :? PhpclassProductActionextendsAction {publicfunctionindex () {echoproduct module, how does ind add a 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 through 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: you can control them by using the URL_MODEL parameter.


1. normal mode test: supported by default


Www.tp.com/index.php? M = Product & a = index & id = 1

M module name

Action


2. pathinfo mode (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 ..... The following parameters are usually paired. Separated.


3. rewrite mode, that is, rewrite. Used in static page

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 to All (note that it cannot appear with # Options Indexes at the same time; otherwise, there will be no access permission)


3. make sure that URL_MODEL is set to 2.

4. Save the following content as a. htaccess file and put it under the same directory of 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 the 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 is represented respectively. Supported by default. what is the problem ????


No matter what mode supports normal mode, you can see the difference when submitting a form.

Path separator: 'URL _ PATHINFO_DEPR '=>'-', // delimiter used between parameters in PATHINFO mode


Http://www.tp.com /? S = Product-index-id-1-name-zhangsan

What are the null operations in the controller? This method can simplify the url and take advantage of its features.

The system cannot find the specified method and the method to execute.


Add the following method to the controller:

/*
* $ Name indicates the request method.
*/
Public function _ empty ($ name ){
Echo 'request page cannot display '. $ name;
}


5. empty module

When the system cannot find the specified module name, the system attempts to locate the empty module (EmptyAction ), this mechanism can be used to customize error pages and optimize URLs.

L EmptyAction

MODULE_NAME

Now we try to request a url

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

Because the StudentAction controller is not in our system, an error is reported.

We create a class named EmptyAction in the project. if the system cannot find the corresponding module, it will automatically locate the Action, if we add another method named _ empty in this class, we can block all url errors.


5. Project Group

In a large project, a large project is usually composed of several small projects. For example

Foreground project, background Project, member blog, forum

L config. php

L APP_GROUP_LIST group list

L DEFAULT_GROUP default group

In our project, start grouping now:

1) foreground project Home

2) Admin of the background Project

Why? Add a new controller in Lib/Action: ProductAction. class. php :? Php class ProductAction extends Action {public function index () {echo product module, ind...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.