Tp use the holiday to learn about simple framework.
- Entry file
tp5 file comes inlet, located in public / index.php, the file content comprises
? < PHP // [Application inlet File] // Define application catalog DEFINE ( 'APP_PATH', __DIR__ '/../application/'. ); // Load the file frame guide the require . __DIR__ '/../thinkphp/start .php ' ; ?>
This action is to define a code entry address, e.g. localhost / tp5 / public
- Controller
Each module has its own controller, the controller is located, for example, application index file Index.php
In the code, we can make the following changes
<?php namespace app\index\controller; class Index { public function index($name = 'World') { return 'Hello,' . $name . '!'; }
For example, when accessing url localhost.tp5.com?name = ....
Parameters Parameter name is entered controllable
When a controller which functions to access any subfolders are required to enter one of the controllers
- URL and routing
url to access unified entrance url/index.php/模块/控制器/操作
For example, we enter www.tp5.com first to enter the URL that is www.tp5.com/index.php
I.e., if desired to control the entry address of the access controller needs the inserted index.php
<?php namespace app\index\controller; class Index { public function index() { return 'index'; } public function hello($name='') { return 'love,' .$name. '!'; } }
Input url / index.php / index / index / hello / name / hsy in the url
Output: love hsy
So if you want to access the index under subprogram module first you need to enter the controller belongs.
.htaccess
This configuration file is mainly used to rewrite the root directory, when a user visits the site, this configuration file can be substituted into the user needs to jump page.
- Define the route
When you define a file location as above rules, you need to enter his controller, entering the specified file.
If a function is very complex site, the file contains a controller in each will be more, so the definition of the route which can simplify the process.
return [ // add routing rule to index method of operating a controller hello 'hello /: name' => 'index / index / hello', ];
The routing rules represent all hello
the beginning and access parameters are routed to index
the controller of the hello
operation method.
Earlier url address to access index.php / index / index / hello / name / world
You can now directly reduced to index.php / hello / world
Route parameters:return [ // 定义路由的请求类型和后缀 'hello/[:name]' => ['index/hello', ['method' => 'get', 'ext' => 'html']], ];
Here limiting the request method to get and only ending with .html files.
Routing variables
<?php namespace app\index\controller; class Blog { public function get($id) { return 'cheak id='.$id; } public function read($name) { return 'check name='.$name; } public function archieve($year,$month) { return 'check'.$year.'/' .$month; } }
Add find items within a controller
Add the application in the following route.php
return [ 'blog/:year/:month' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']], 'blog/:id' => ['blog/get', ['method' => 'get'], ['id' => '\d+']], 'blog/:name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']], ];
Visit url / blog / 5 Finding id = 5