TP5 URLs and Routing

application\index\controller\Index.php

<?php
namespace app\index\controller;
class Index extends Controller
{
     public function hello($name = 'World')
    {
        return 'Hello,' . $name . '!';
    }
}

 

application\index\controller\Blog.php

<?php
namespace app\index\controller;
class Blog
{
    public function get($id)
    {
        return 'View id=' . $id . ' content';
    }
    public function read($name)
    {
        return 'View name=' . $name . ' content';
    }
    public function archive($year, $month)
    {
        return 'View the archive of' . $year . '/' . $month . ';
    }
}

 

application\route.php routing configuration

<?php
return [
	//http://tp5.com/hello output: Hello, World!
    //http://tp5.com/hello/onestopweb output: Hello,onestopweb!
    //'hello/[:name]' => 'index/hello',
    //http://tp5.com/hello.html Output: Hello, World!
    //http://tp5.com/hello/onestopweb.html Output: Hello, onestopweb!
    'hello/[:name]' => ['index/hello', ['method' => 'get', 'ext' => 'html']],
    
    //http://tp5.com/blog/5 Output: View the content of id=5
    //'blog/:id'          => ['blog/get', ['method' => 'get'], ['id' => '\d+']],
    //http://tp5.com/blog/onestopweb output: http://tp5.com/blog/onestopweb
    //'blog/:name'        => ['blog/read', ['method' => 'get'], ['name' => '\w+']],
    //http://tp5.com/blog/2015/05 Output: http://tp5.com/blog/2015/05
	//'blog/:year/:month' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']],
	//http://tp5.com/blog-2015-06 Output: http://tp5.com/blog-2015-06
    //'blog-<year>-<month>' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']],
    
    // global variable rule definition
    '__pattern__'         => [
        'name'  => '\w+',
        'id'    => '\d+',
        'year'  => '\d{4}',
        'month' => '\d{2}',
    ],
    // routing rule definition
    //http://tp5.com/blog/5 Output: View the content of id=5
    'blog/:id'            => 'blog/get',
    //http://tp5.com/blog/onestopweb output: http://tp5.com/blog/onestopweb
    'blog/:name'          => 'blog/read',
    //http://tp5.com/blog-2015-06 Output: http://tp5.com/blog-2015-06
    'blog-<year>-<month>' => 'blog/archive',
    
	// Define local variable rules
	//http://tp5.com/blog/onest is greater than or equal to length 5 Output: View the content of name=onest
    //'blog/:name'          => ['blog/read', ['method' => 'get'], ['name' => '\w{5,}']],
    
    //hint:
    //http://tp5.com/blog/5.html is the same as: http://tp5.com/blog/5 Output: View the content of id=5
];

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326556195&siteId=291194637