Home > Backend Development > PHP Tutorial > THINKPHP controller_PHP tutorial

THINKPHP controller_PHP tutorial

WBOY
Release: 2016-07-14 10:12:13
Original
1056 people have browsed it


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...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template