内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

PHPDIY系列之自定義配置和路由


我們已經(jīng)開(kāi)發(fā)完成,但我們還需要更多。比如自定義配置和路由。

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:國(guó)際域名空間、網(wǎng)絡(luò)空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、棲霞網(wǎng)站維護(hù)、網(wǎng)站推廣。

app文件夾下新建Config.php

<?php/**
 *自定義配置
 */return [
    'debug' => false,
    'route' => [
        '' => 'demo/welcome',
        'test' => 'demo/test',
    ],];

新建DemoController(app/Https/Controllers目錄下)

<?php/**
 * Demo控制器
 */namespace App\\Https\\Controllers;use Library\\Https\\Controller;class DemoController extends Controller{
    public function welcome($params)
    {
        return $this->response->json(['hello' => 'welcome']);
    }

    public function test($params)
    {
        return $this->response->json($params);
    }}

修改入口文件index.php,加入加載配置代碼:

... 省略代碼
// 加載配置
$config = require SF_LIBRARY_PATH.'Config.php';
$appConfig = file_exists($appConfigPath = SF_APP_PATH.'Config.php') ? require $appConfigPath : [];
$config = array_merge($config, $appConfig);
$config['debug'] = ($config['debug']?? SF_DEBUG);
...省略代碼

解析路由部分也加入自定義路由處理:

// Application...省略代碼
public function handleRequest(Request $request){
    $route = $request->resolve($this->_config['route']??[]);

    $response = $request->runAction($route);
    /**
     * 執(zhí)行結(jié)果賦值給$response->data,并返回給response對(duì)象
     */
    if ($response instanceof Response) {
        return $response;
    }

    throw new SaiException('Content format error');}
    ...省略代碼
    public function resolve($route=[])  {$this->route = $route;  // 自定義路由return $this->getPathUrl();  }
    // Request
    ...省略代碼public function runAction($route){
    if (array_key_exists($route, $this->_route)) {
        $route = $this->_route[$route];
    }

    $match = explode('/', $route);
    $match = array_filter($match);
    ...省略代碼

保存后打開(kāi)瀏覽器看看效果:

這里雖然有自定義路由,但是我們有時(shí)候需要禁止默認(rèn)路由,所以我們不妨增加配置參數(shù)defaultRoute,用來(lái)控制是否開(kāi)啟默認(rèn)路由。

我們修改一下路由解析的代碼:

//Application...省略代碼
public function handleRequest(Request $request){
    $route = $request->resolve($this->_config['route']??[]);

    $response = $request->runAction($route, $this->_config['defaultRoute']??true);
    /**
     * 執(zhí)行結(jié)果賦值給$response->data,并返回給response對(duì)象
     */
    if ($response instanceof Response) {
        return $response;
    }

    throw new SaiException('Content format error');}
    ...省略代碼
...省略代碼
public function runAction($route, $defaultRoute){
    if (array_key_exists($route, $this->_route)) {
        $route = $this->_route[$route];
    } elseif (!$defaultRoute) {
        throw new NotFoundException("route not found:".$route);
    }
    ...省略代碼

我們?cè)赼pp下面的Config,加入:

return [
    'debug' => false,
    'route' => [
        '' => 'demo/welcome',
        'test' => 'demo/test',
    ],
    'defaultRoute' => false,];

我們打開(kāi)瀏覽器輸入saif.com/login

報(bào)錯(cuò)如下:

Array
(
    [line] => 137
    [msg] => route not found:login
    [code] => 404
    [file] => library/Https/Request.php
)

名稱(chēng)欄目:PHPDIY系列之自定義配置和路由
當(dāng)前地址:http://m.rwnh.cn/article10/cgeodo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、微信公眾號(hào)微信小程序、品牌網(wǎng)站設(shè)計(jì)、關(guān)鍵詞優(yōu)化、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站
新乡县| 温宿县| 茌平县| 武安市| 南木林县| 彩票| 原平市| 徐汇区| 乌拉特前旗| 西林县| 汕头市| 朝阳市| 沅陵县| 城步| 临颍县| 濉溪县| 衡山县| 宁南县| 滕州市| 崇州市| 集贤县| 秦安县| 永福县| 大连市| 岐山县| 达孜县| 武安市| 祥云县| 收藏| 兴隆县| 澄迈县| 赫章县| 平原县| 延津县| 深泽县| 高平市| 嘉兴市| 个旧市| 平乐县| 平安县| 杭锦后旗|