1. Separation of front and back ends
a. Create a new admin.php in the public directory of the website
b. Open admin.php
<?php
// [application entry file]
// define application directory
define('APP_PATH', __DIR__ . '/../application/');
// load framework boot file
require __DIR__ . '/../thinkphp/start .php';
2. Binding modules
1. Realize the function
The entry file index.php can only go to the front-end module
admin.php This entry file can only go to the background module. It is recommended that the entry file in the background be a little more complicated
2. How to implement
in the entry file
define ('BIND_MODULE','index');//Bind the foreground module
define ('BIND_MODULE','admin'); // Bind the background module
3. url address
a. Before the entry binding module
http://www.tp5.com/entryfile/module/controller/action
b. After the entry binding module
http://www.tp5.com/entryfile/controller/action
3. Hide the entry file
1. Start apache rewriting F:\phpStudy\PHPTutorial\Apache\conf\httpd.conf
# remove the comment
更改后:LoadModule rewrite_module modules/mod_rewrite.so
2. Set the access permission AllowOverride none to All
3. The entry file, create a new .htaccess in the public directory of the website
//The first writing method of phpstudy
<IfModule mod_rewrite.c> Options +FollowSymlinks - Multiviews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </IfModule>
//If the first method does not work well, use the second method
<IfModule mod_rewrite.c> Options +FollowSymlinks - Multiviews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] </IfModule>
4. Restart the service
5. URL address change
a. Before hiding
http://www.tp5.com/index.php/Index/test
b. After hiding
http://www.tp5.com/Index/test
tp5.0 routing learning Note: The advantages of routing: 1. Simplify the url address, easy to remember 2. Conducive to the optimization of search engines
Route::rule('route expression','route address','request type','route parameter(array)','variable rule(array)');
1. Support three ways of url parsing rules
2. Routing is only for applications, not modules, so routing settings are also for all modules under the application.
3. If some modules do not want to use routing to close the background module, add the following sentence to the background entry file and write it in
After loading the framework bootstrap file, otherwise an error will be reported
// Load framework bootstrap file require __DIR__ . '/../thinkphp/start.php';
// Close the route under the admin module \think\App::route( false );
4. Routing mode
a. Normal mode
1. Turn off routing and completely use the default PATH_INFO URL
2. Form: http://www.tp5.com/admin.php/Index/index
3. How to configure:
//Whether to enable routing
‘url_route_on’ => false,
//Whether to force the use of routing
'url_route_must' => false,
b. Mixed mode
1. Definition: Enable routing and use a mix of routing definition + default PATH_INFO mode
2. How to set
//Whether to enable routing
‘url_route_on’ => true,
//Whether to force the use of routing
'url_route_must' => false,
c. Forced mode
1. Definition:
Turn on routing and set the route must be defined to access
2. How to set
//Whether to enable routing
‘url_route_on’ => true,
//Whether to force the use of routing
'url_route_must' =>true,
5. Set up routing - dynamic single registration
a. Set up the routing file
application\route.php
b. How to set
// Introduce the system class use think\Route; // Define routing rules
//After setting the route, you cannot use the PATH_INFO mode to access
//Register the route to access the index method of the index module Index controller Route::rule('/','index/Index/index');
//Register route access to index module Index controller test method
Route::rule('test','index/Index/test');
c, the form of routing
1. Static address routing
// Register the route test to access the index controller test method of the Index module
Route::rule('test','index/Index/test');
2. Bring parameters to the route
//Register the route with parameters
//http://www.tp5.com/course/1 routing mode
//http://www.tp5.com/index/Index/course/id/1 Normal mode
Route::rule('course/:id','index/Index/course');
//If the route sets two parameters, it must take two parameters
Route::rule('time/:year/:mouth','index/Index/shijian');
3. Optional parameter routing
//http://www.tp5.com/time/2017
//http://www.tp5.com/time/2018/5
Route::rule('time/:year/[:mouth]','index/Index/shijian');
4. Full dynamic routing (not recommended)
Route::rule(':a/:b','index/Index/dongtai');
5. Exact match routing
//http://www.tp5.com/wanquan can access
//http"//www.tp5.com/wanquan/ada/asf/a/f cannot access
Route::rule('wanquan$',' index/Index/wanquan');
6. Routing with additional parameters
Route::rule('test1','index/Index/test1?id=12&name=adasfds');
d. Set the request type
1. Request type in tp
get、post、put、delete
2. Route::rule() supports all request types by default
3. Set all requests
// Support get request Route::rule('type','index/Index/type','get' ); Route::get('type','index/Index/type'); // Support post request Route::rule('type','index/Index/type','post' ); Route::post('type','index/Index/type'); // Support both get and post Route::rule('type','index/Index/type','get|post' ); // Support all requests Route::rule('type','index/Index/type','*' ); Route::any('type','index/Index/type'); // Support put request Route::rule('type','index/Index/type','put' ); Route::put('type','index/Index/type'); // Support delete request Route::rule('type','index/Index/type','delete' ); Route::delete('type','index/Index/type');
4. How to simulate put and delete requests
<form action="" method="post"> post 重要 <p> <input type="hidden" name="_method" value="PUT"> **隐藏域重要 <input type="text" name="name"> </p> <p> <input type="submit" value="提交"> </p> </form>
6. Set routing - dynamic batch registration
a. Basic format
Route:: rule([ 'Route rule 1'=>'Route address and parameters', 'Routing rule 2'=>['routing address and parameters','matching parameters (array)','variable rules (array)' ] ... ] ,'','request type','matching parameters (array) ','Variable rule');
b. to use
Route::rule([ 'test' => 'index/Index/test', 'course/:id' => 'index/Index/course' ]);
Route::get([ 'test' => 'index/Index/test', 'course/:id' => 'index/Index/course' ]);
7. Set routing - batch registration of configuration files
return [ 'test' => 'index/Index/test', 'course/:id' => 'index/Index/course' ];
8. Variable rules
//Set the route parameter id must be a number, must be 1 to 3 digits
Route::rule('course/:id','index/Index/course','get',[],["id" => "\ d{1,3}"]);
9. Routing parameters
//The route parameter method request method must be get
//The route parameter ext mainly sets the suffix of the
route Route::rule('course/:id','index/Index/course','get',['method'=> 'get','ext'=>'html'],["id" => "\d{1,3}"]);
10. Resource routing
a. Background function
add page, display page, delete function, modify page, modify function, add function
a. Declare resource routing
Route::resource('blog','index/Blog');
b. Seven routing rules will be automatically registered
11. Set up shortcut routes
a. Statement
Route::controller('user','index/User');
b. Use:
namespace app\index\controller; class User { public function getInfo() { } public function getPhone() { } public function postInfo() { } public function putInfo () { } public function deleteInfo() { } }
c. URL access
get http://localhost/user/info get http://localhost/user/phone post http://localhost/user/info put http://localhost/user/info delete http://localhost/user/info
12. Generate URL address
a. System class
use think\url
url::build('index/Index/index');
b. System approach
url('index/Index/index');
c. to use
See the manual for details