TP5 URL和路由

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 '查看id=' . $id . '的内容';
    }
    public function read($name)
    {
        return '查看name=' . $name . '的内容';
    }
    public function archive($year, $month)
    {
        return '查看' . $year . '/' . $month . '的归档内容';
    }
}

application\route.php 路由配置

<?php
return [
	//http://tp5.com/hello 输出:Hello,World!
    //http://tp5.com/hello/onestopweb 输出:Hello,onestopweb!
    //'hello/[:name]' => 'index/hello',
    //http://tp5.com/hello.html 输出:Hello,World!
    //http://tp5.com/hello/onestopweb.html 输出:Hello,onestopweb!
    'hello/[:name]' => ['index/hello', ['method' => 'get', 'ext' => 'html']],
    
    //http://tp5.com/blog/5 输出:查看id=5的内容
    //'blog/:id'          => ['blog/get', ['method' => 'get'], ['id' => '\d+']],
    //http://tp5.com/blog/onestopweb 输出:http://tp5.com/blog/onestopweb
    //'blog/:name'        => ['blog/read', ['method' => 'get'], ['name' => '\w+']],
    //http://tp5.com/blog/2015/05 输出: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 输出:http://tp5.com/blog-2015-06
    //'blog-<year>-<month>' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']],
    
    // 全局变量规则定义
    '__pattern__'         => [
        'name'  => '\w+',
        'id'    => '\d+',
        'year'  => '\d{4}',
        'month' => '\d{2}',
    ],
    // 路由规则定义
    //http://tp5.com/blog/5 输出:查看id=5的内容
    'blog/:id'            => 'blog/get',
    //http://tp5.com/blog/onestopweb 输出:http://tp5.com/blog/onestopweb
    'blog/:name'          => 'blog/read',
    //http://tp5.com/blog-2015-06 输出:http://tp5.com/blog-2015-06
    'blog-<year>-<month>' => 'blog/archive',
    
	// 定义了局部变量规则
	//http://tp5.com/blog/onest 大于等于长度5 输出:查看name=onest的内容
    //'blog/:name'          => ['blog/read', ['method' => 'get'], ['name' => '\w{5,}']],
    
    //提示:
    //http://tp5.com/blog/5.html 等于同:http://tp5.com/blog/5 输出:查看id=5的内容
];

猜你喜欢

转载自onestopweb.iteye.com/blog/2384506