當前位置:首頁 » 編程語言 » php框架路由

php框架路由

發布時間: 2022-05-01 05:01:44

⑴ thinkphp框架5.0路由怎麼使用

點擊4個不同的路由地址,可以查看當前的路由情況。

路由到read操作
路由到archive操作
項目配置文件中的路由定義如下:
//啟用路由功能
'URL_ROUTER_ON'=>true,
//路由定義
'URL_ROUTE_RULES'=> array(
'blog/:year\d/:month\d'=>'Blog/archive', //規則路由
'blog/:id\d'=>'Blog/read', //規則路由
'blog/:cate'=>'Blog/category', //規則路由
'/(\d+)/' => 'Blog/view?id=:1',//正則路由
),
在模板文件中,我們使用了U函數動態生成路由地址:
路由1:blog/curd
路由2:blog/5
路由3:blog/2012/09
路由4:100

⑵ php怎麼學會一個框架的路由機制

如果用過zend和symphony或者yii的可以去研究一下源碼
主要是通過偽靜態來實現單一入口,mvc框架都是這樣。將所有請求通過apache偽靜態解析轉給某個指定文件,然後通過php的$_SERVER[]這個全局變數來去的請求的路徑字元串,對其進行解析再分配給指定的類去處理,基本就是這樣。
最簡單的方法是建立一個.htaccess文件。文件內容貼給你
<IfMole !mod_rewrite.c>
ErrorDocument 500 'mod_rewrite must be enabled'
</IfMole>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php

將這個文件放在根目錄下即可。文件作用很明顯:將所有的請求路徑轉給index.php,然後在index.php里做請求字元串的判斷即可打到路由解析功能。

⑶ php框架 laravel 路由裡面的 [user=>]是什麼意思

寫錯了吧,後面應該是['uses'=>'Controller@方法'],意思是前面的url指向後面的方法

⑷ 很多php框架裡面的Route和Controller是干嗎的

Route就是路由,基本就是用來指明調用哪個Controller用的,

Controller就是控制器,處理用戶交互的部分

⑸ PHP的路由是什麼 還有什麼通俗的說法嗎

你所說的路由其實和路由器是一個道理, 通過一個入口接受請求, 然後通過(URL)匹配規則將請求分發到不同的地方。具體到一些主流框架上面,Router模塊會配合Http模塊分析請求, 並且按照一定規則解析去匹配路由,然後使用調度模塊使邏輯調到某塊代碼(通常是控制器),最後返回響應(Response)。
所以說你就把這種路由當做家裡用來上網的路由器, 道理是一樣的。

⑹ 如何在PHP中實現URL路由

第一步,首先要在伺服器的配置上對/router/路徑進行攔截

調用某個文件夾目錄下的index.php頁面,假定現在所有模塊使用單獨的文件存放於class目錄下,該目錄與router平級,如下圖所示:

第二步,路由分發器的實現(index.php)
1: <!Doctype html>
2: <html>
3: <head>
4: <title>路由測試~~</title>
5: <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6: </head>
7: <body>
8:
9: <?php
10:
11: date_default_timezone_set("Asia/Shanghai");
12:
13: define("MODULE_DIR", "../class/");
14:
15:
16: $_DocumentPath = $_SERVER['DOCUMENT_ROOT'];
17: $_FilePath = __FILE__;
18: $_RequestUri = $_SERVER['REQUEST_URI'];
19:
20: $_AppPath = str_replace($_DocumentPath, '', $_FilePath); //==>\router\index.php
21: $_UrlPath = $_RequestUri; //==>/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
22:
23: $_AppPathArr = explode(DIRECTORY_SEPARATOR, $_AppPath);
24:
25: /**
26: * http://192.168.0.33/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
27: *
28: * /hello/router/a/b/c/d/abc/index.html?id=3&url=http:
29: */
30:
31: for ($i = 0; $i < count($_AppPathArr); $i++) {
32: $p = $_AppPathArr[$i];
33: if ($p) {
34: $_UrlPath = preg_replace('/^\/'.$p.'\//', '/', $_UrlPath, 1);
35: }
36: }
37:
38: $_UrlPath = preg_replace('/^\//', '', $_UrlPath, 1);
39:
40: $_AppPathArr = explode("/", $_UrlPath);
41: $_AppPathArr_Count = count($_AppPathArr);
42:
43: $arr_url = array(
44: 'controller' => 'index',
45: 'method' => 'index',
46: 'parms' => array()
47: );
48:
49: $arr_url['controller'] = $_AppPathArr[0];
50: $arr_url['method'] = $_AppPathArr[1];
51:
52: if ($_AppPathArr_Count > 2 and $_AppPathArr_Count % 2 != 0) {
53: die('參數錯誤');
54: } else {
55: for ($i = 2; $i < $_AppPathArr_Count; $i += 2) {
56: $arr_temp_hash = array(strtolower($_AppPathArr[$i])=>$_AppPathArr[$i + 1]);
57: $arr_url['parms'] = array_merge($arr_url['parms'], $arr_temp_hash);
58: }
59: }
60:
61: $mole_name = $arr_url['controller'];
62: $mole_file = MODULE_DIR.$mole_name.'.class.php';
63: $method_name = $arr_url['method'];
64:
65: if (file_exists($mole_file)) {
66: include $mole_file;
67:
68: $obj_mole = new $mole_name();
69:
70: if (!method_exists($obj_mole, $method_name)) {
71: die("要調用的方法不存在");
72: } else {
73: if (is_callable(array($obj_mole, $method_name))) {
74: $obj_mole -> $method_name($mole_name, $arr_url['parms']);
75:
76: $obj_mole -> printResult();
77: }
78: }
79:
80: } else {
81: die("定義的模塊不存在");
82: }
83:
84:
85: ?>
86:
87: </body>
88: </html>

獲取請求的uri,然後拿到要載入的模塊名、調用方法名,對uri參數進行簡單的判斷..

第三步,模塊的編寫
根據上述的uri,我們要調用的是Hello模塊下的router方法,那麼可以在class目錄下定義一個名為Hello.class.php的文件(注意linux下是區分大小寫的)
1: <?php
2:
3: class Hello {
4:
5: private $_name;
6: private $_varValue;
7:
8: function __construct() {
9:
10: }
11:
12: function router() {
13: $this->_name = func_get_arg(0);
14: $this->_varValue = func_get_arg(1);
15: }
16:
17: function printResult() {
18: echo $this->_name;
19: echo "<p>";
20: echo var_mp($this->_varValue);
21: echo "</p>";
22: }
23: }
24:
25: ?>

⑺ 我想嘗試著寫個小型PHP框架,現在卡在類自動載入和路由實現上了

類自動載入跟路由是框架最基礎的特性

給你點自動載入的思路,首先現在寫框架必須用上命名空間,框架內部的類文件夾根據命名空間命名方便自動載入

在入口文件引入自己寫的Autoload.php 通過spl_autoload_register獲得要載入的類名

spl_autoload_register(function($class_name){
Autoload::splAutoload($class_name);
});

asseek outeRouter


asseekFunc


asseek outeRoute


appwwwdocsdocsController


asseekController


asseekView


如果你採用命名空間上面函數的$class_name是類似這樣的字元串,只要你有當前項目的初始路徑再根據這段字元串require相應的php文件應該不難,自動載入就搞定了。

路由就更簡單了就是通過$_SERVER['REDIRECT_URL'] 或$_SERVER['REDIRECT_URI'] 獲得用戶訪問的URL,根據自己的框架制定的規則從URL中取得控制器與控制器方法名稱,判斷controller的類是否存在,存在則實例化,再判斷controller的method是否存在,存在則調用方法,方法裡面載入視圖等就是另外的事情了。

熱點內容
編譯原理聖經級書 發布:2024-04-28 16:48:21 瀏覽:146
我的世界手機版如何在伺服器上 發布:2024-04-28 16:35:21 瀏覽:862
pythonwindowsweb 發布:2024-04-28 16:10:29 瀏覽:542
王牌競速如何找到最開始的伺服器 發布:2024-04-28 14:53:09 瀏覽:403
airpod安卓怎麼切換下一曲 發布:2024-04-28 14:23:03 瀏覽:835
百姓網源碼 發布:2024-04-28 14:18:56 瀏覽:893
war包防止反編譯 發布:2024-04-28 14:17:16 瀏覽:328
linuxll命令 發布:2024-04-28 14:16:27 瀏覽:860
阿里雲伺服器增強安全配置取消 發布:2024-04-28 14:16:12 瀏覽:867
war3存儲空間不足 發布:2024-04-28 13:20:54 瀏覽:949