Blogger Information
Blog 40
fans 1
comment 0
visits 32434
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
框架的配置与路由
李明伟的博客
Original
1018 people have browsed it
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
//http://tp51.io/index/test/demo1


use think\facade\Route;

//路由到模块/控制器/操作
Route::rule('demo1','index/test/demo1')//rule方法将原有的地址路径映射到rule中,此时原本的路径失效
        ->method('get');//method用于设置请求方法

//路由到一个闭包函数
Route::rule('demo2',function (){
    return 'tp框架';
})->method('get');//可以使用rule方法将一个闭包函数映射到rule上

//若开启了强制路由,首页也要定义路由规则
Route::rule('/','index/index/index')
        ->method('get');

//路由变量
//Route::rule('route1/:name/[:course]/[:grade]','index/test/route1')
//        ->method('get');//在rule中填写参数时需要在变量名前加,添加方括号代表该变量为可选变量

//自定义路由表达式的分隔符
//http://tp51.io/route1/peter/java/90
//Route::rule('route1-:name-[:course]-[:grade]','index/test/route1')
//      ->method('get');//用'-'代替'/'

//路由变量的新的语法
/*Route::rule('route1-<name>-<course?>-<grade?>','index/test/route1')*/
//        ->method('get');//用'<>'代替':'表示变量'?'代替'[]'表示变量可选

//对路由变量中的变量进行约束与验证
Route::rule('route1-<name>-<course?>-<grade?>','index/test/route1')
    ->method('get')
    ->pattern(['name'=>'[a-zA-Z]+','course'=>'[a-zA-z]+','grade'=>'[0-9]+']);//'/d'可代替[0-9']
    //pattern方法可对路由变量进行约束与验证

//路由参数
Route::rule('route2','index/test/route2')
    ->method('get')
    ->ext('html|shtml|png');//URL后缀检测,exc方法支持同时设置多个后缀,使用'|'隔开,也可使用链式调用,设置为空后表示不允许添加URL后缀

//URL生成
Route::rule('route3','index/test/route3')
    ->method('get');

//请求对象
Route::rule('route4','index/test/route4')
    ->method('get');

以上是路由文件

一下是对应的控制器文件

<?php
/**
 * Created by PhpStorm.
 * User: 62505
 * Date: 2019/3/24
 * Time: 12:11
 */
//php配置的基本操作
namespace app\index\controller;

use think\facade\Url;
use think\facade\Config;
use think\Controller;
use think\facade\Request;

class Test extends Controller
{
    //配置的读取
    public function demo1()
    {
        //读取一个配置项
        $res = Config::get('database.database');

        //获取某个一级配置下面的所有配置项
        $res = Config::pull('database');//推荐使用pull方法
        $res = Config::get('database.');

        //检测是否存在某个配置项
        $res = Config::has('database.database');
        //database.指代配置项所在的配置,若未填写则为默认配置app.php

        //设置
        Config::set('app.app_name','MyApp');
        $res = Config::get('app_name');

        dump($res);//框架提供的打印函数
    }

    //路由变量
    public function route1($name,$course = 'php',$grade = 80){
        return $name."的 ".$course."课程的成绩是".$grade;
    }

    //路由参数
    public function route2(){
        return '路由参数检测';
    }

    //URL生成
    public function route3(){
        $url = Url::build('index/test/route1',['name'=>'zhu','course'=>'java','grade'=>'98']);
        $url = Url::build('index/route1/zhu/java/98');//URL的build方法也可以构造路由地址
        dump($url);
    }

    //请求对象与常用方法
    public function route4(){
        $res = Request::param();
        dump($res);
    }
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!