置頂 簡易PHP路由,支援正反向url解析支援

2021-07-22 17:49:59 字數 3002 閱讀 2132

幾年前實現了乙個簡單的正向路由,那時候不會寫反向路由解析,最近通讀qee v3的**,因為其中已經扣除了路由解析這個功能,故自己結合之前的經驗,發現寫個簡單的正反向路由解析還是蠻簡單的,見**:

<?php

/* * 靜態頁配置檔案

* * key 為 pattern

* 值 為 配置引數

* * 越上面的優先順序越高

*/return array(

array(

'pattern' => 'core--.html',

'action' => 'core/show',

'config' => array(

'id' => '[0-9]+',

'name' => '[a-z][a-z_0-9]+'

),'default' => array(

'id' => 2,

'name' => 'sfken'

),

), array(

'pattern' => 'admin.html',

'action' => 'admin/index',

),);

上面是配置檔案

<?php

class tool_pagestatic

$this->_obj_id = $config['obj_id'];

$this->_accessor = val( $config , 'accessor' , 'page' );

$this->_cache = val( $config , 'cache' , null );

$cfopt = val( $config , 'config' , null );

if ( !empty($cfopt) )

if ( is_file($cfopt) )

}$cfopt = null;

$this->default = array(

'nocache' => true,

'params' => array(),);}

private function loadconfig( array $config )

', '('.$conf_val.')', $opts['pattern']);}}

else

if ( empty($opts['default']) ) $opts['default'] = array();

$this->_config[strtolower($opts['action'])] = $opts;

} # 解析完成後是否可以存入快取 }

function getaccessor()

/*** 解析 string 返回對應的action以及相應的引數

* * @param string $string

* * @return array

*/function parsing($string)

}$d['nocache'] = val($opts,'nocache',$d['nocache']);

$d['action'] = $opts['action'];

break;}}

$this->_result[$string] = $d;

} return $this->_result[$string]; }

/*** 生成靜態的 url 位址

} # 如果沒有找到就生成原始的url

return url($action_name , $params); }

}

上面是真正的實現**

下面是呼叫**

<?php

/** * 站點靜態化操作

* * /static/page/article-php-json_encode.html

* *

*/class action_static extends baseaction

}return false;

}/**

* 請求前對資料進行驗證失敗時呼叫此方法

*/protected function _on_validate_input_failed()

function execute()

}

輸出結果如下:

寫道admin.html :

array (

[action] => admin/index

[nocache] => 1

[params] => array (

) )

core--.html

core-449211678-kenxu.html?age=29

寫道core-12306-tianchao.html :

array (

[action] => core/show

[nocache] => 1

[params] => array (

[id] => 12306

[name] => tianchao )

)core--.html

core-449211678-kenxu.html?age=29

在我當前的設計中 此路由功能只能算是乙個工具類,用於實現 偽靜態/或者頁快取用的,功能上只支援指定action的操作,如果諸位有需要,此處就算我 拋磚引玉了....

**很少,也就不一一解釋了

php置頂文章,zblogphp呼叫置頂文章的教程

中的 10 則標識呼叫的文章數量。這個問題就完美的解決了,但是客戶又問,我能不能指定我想展示的文章呢?然後新一輪的折騰又開始了,起初先看見了天興的部落格,有這個教程,但是這個指定一篇文章,這段 就是zblogphp呼叫指定文章的 如下 post getpost 1 這裡的1是指定的文章id 但是只能...

簡易路由操作

在那些框架上的路由鏈結大概都這樣子index.html index 或者index.html list 12 通過對hash變化的監聽,可以知道我們的路由到 了,然後再分配給應執行的函式 這樣就可以生效。乙個可能的使用方式是 router list function 以上所表述的是 當頁面為inde...

置頂 讓FLEAPHP支援簡易的命名空間

在flea filter 目錄下增加乙個檔案,叫做namespaceuri.php,裡面內容如下 增加命名空間支援 將命名空間掛接到控制器名稱之前 function namespace urlrewrite filter firsttime false foreach as k v pathinfo...