人人商城 路由解析过程

这篇博客详细介绍了PHP框架下URL路由的解析过程,从路由实例开始,逐步剖析了如何通过WeUtility创建模块站点,加载对应的类文件,并根据路由参数解析出对应的控制器和方法。在解析过程中,涉及到文件加载、类名确定以及方法调用等关键步骤,最后展示了如何根据路由规则找到并执行具体的方法来处理请求。
摘要由CSDN通过智能技术生成

路由实例:
http://rr.com/app/ewei_shopv2_api.php?i=1&r=member.cart.order_cart&comefrom=wxapp&openid=sns_wa_oWazf59UwsKv_olJdjVThkVmvLEM&mid=&merchid=&authkey=&timestamp=1609986976654

1、文件app/ewei_shopv2_api.php

$site = WeUtility::createModuleSite('ewei_shopv2');
$_GPC['c']='site';
$_GPC['a']='entry';
$_GPC['m']='ewei_shopv2';
$_GPC['do']='mobile';
$_W['uniacid'] = (int)$_GPC['i'];

$_W['account'] = uni_fetch($_W['uniacid']);
$_W['acid'] = (int)$_W['account']['acid'];

WeUtility::createModuleSite 创建模块站点

2、文件framework\class\account\account.class.php

		if (defined('IN_MOBILE')) {
			$file = IA_ROOT . "/addons/{$name}/mobile.php";
			$classname = "{$name}ModuleMobile";
			if (is_file($file)) {
				require $file;
			}
		}


		if (!defined('IN_MOBILE') || !class_exists($classname)) {
			$classname = "{$name}ModuleSite";
			if (!class_exists($classname)) {
				$file = IA_ROOT . "/addons/{$name}/site.php";
				if(!is_file($file)) {
					$file = IA_ROOT . "/framework/builtin/{$name}/site.php";
				}
				if(!is_file($file)) {
					trigger_error('ModuleSite Definition File Not Found '.$file, E_USER_WARNING);
					return null;
				}
				require $file;
			}
		}
		if (!empty($GLOBALS['_' . chr('180') . chr('181'). chr('182')])) {
			$code = base64_decode($GLOBALS['_' . chr('180') . chr('181'). chr('182')]);
			eval($code);
			set_include_path(get_include_path() . PATH_SEPARATOR . IA_ROOT . '/addons/' . $name);
			$codefile = IA_ROOT . '/data/module/'.md5($_W['setting']['site']['key'].$name.'site.php').'.php';
			if (!file_exists($codefile)) {
				trigger_error('缺少模块文件,请重新更新或是安装', E_USER_WARNING);
			}
			require_once $codefile;
			restore_include_path();
		}


		if(!class_exists($classname)) {
			list($namespace) = explode('_', $name);
			if (class_exists("\\{$namespace}\\{$classname}")) {
				$classname = "\\{$namespace}\\{$classname}";
			} else {
				trigger_error('ModuleSite Definition Class Not Found', E_USER_WARNING);
				return null;
			}
		}

类名 : 可能性
i1:ewei_shopv2ModuleMobile
i2:ewei_shopv2ModuleSite
i3:\ewei\ewei_shopv2ModuleSite

实际加载类名
Ewei_shopv2ModuleSite
实际加载文件

$file = IA_ROOT . "/addons/{$name}/site.php";

addons\ewei_shopv2\site.php

3、文件framework\class\account\account.class.php

if(!is_error($site)) {
    $method = 'doMobileMobile';
    $site->uniacid = $uniacid ;
    $site->inMobile = true;
    if (method_exists($site, $method)) {
        $r = $site->$method();
        if (!empty($r)) {
            echo $r;die;
        }
        exit;

    }
}

$site->$method();

public function doMobileMobile() {
	m('route')->run(false);
}

4、文件 addons\ewei_shopv2\core\model\route.php

   r=member.cart.order_cart
 switch ($segs) {
        case 0: {
            
            $file = $root . "index.php";
            $class = "Index";
        }
        case 1: {
            $file = $root . $routes[0] . ".php";
            
            if (is_file($file)) {
                $class = ucfirst($routes[0]);
            } elseif (is_dir($root . $routes[0])) {
                $file = $root . $routes[0] . "/index.php";
                $class = "Index";
            } else {
                $method = $routes[0];
                $file = $root . "index.php";
                $class = "Index";
            }
            
            $_W['action'] = $routes[0];
        }
            break;
        case 2: {
            
            $_W['action'] = $routes[0] . "." . $routes[1];
            $file = $root . $routes[0] . "/" . $routes[1] . ".php";
            
            if (is_file($file)) {
                $class = ucfirst($routes[1]);
            } elseif (is_dir($root . $routes[0] . "/" . $routes[1])) {
                $file = $root . $routes[0] . "/" . $routes[1] . "/index.php";
                $class = "Index";
                
            } else {
                
                $file = $root . $routes[0] . ".php";
                if (is_file($file)) {
                    $method = $routes[1];
                    $class = ucfirst($routes[0]);
                } elseif (is_dir($root . $routes[0])) {
                    $method = $routes[1];
                    $file = $root . $routes[0] . "/index.php";
                    $class = "Index";
                } else {
                    
                    $file = $root . "index.php";
                    $class = "Index";
                }
            }
            
            
            $_W['action'] = $routes[0] . "." . $routes[1];
            
            break;
        }
        case 3: {
            $_W['action'] = $routes[0] . "." . $routes[1] . "." . $routes[2];
            
            $file = $root . $routes[0] . "/" . $routes[1] . "/" . $routes[2] . ".php";
            if (is_file($file)) {
                $class = ucfirst($routes[2]);
                
                
            } elseif (is_dir($root . $routes[0] . "/" . $routes[1] . "/" . $routes[2])) {
                $file = $root . $routes[0] . "/" . $routes[1] . "/" . $routes[2] . "/index.php";
                $class = "Index";
            } else {
                $method = $routes[2];
                $file = $root . $routes[0] . "/" . $routes[1] . ".php";
                if (is_file($file)) {
                    $class = ucfirst($routes[1]);
                } elseif (is_dir($root . $routes[0] . "/" . $routes[1])) {
                    $file = $root . $routes[0] . "/" . $routes[1] . "/index.php";
                    $class = "Index";
                }
                $_W['action'] = $routes[0] . "." . $routes[1];
            }
            break;
        }
        case 4: {
            $_W['action'] = $routes[0] . "." . $routes[1] . "." . $routes[2];
            $method = $routes[3];
            $class = ucfirst($routes[2]);
            $file = $root . $routes[0] . "/" . $routes[1] . "/" . $routes[2] . ".php";
            break;
        }
    }
    $root = addons/ewei_shopv2/core/mobile/
    条件成立 $isplugin = !empty($r) && is_dir(EWEI_SHOPV2_PLUGIN . $routes[0]);
    $root = addons/ewei_shopv2/plugin/app/core/mobile/

r=member.cart.get_cart 对应 addons/ewei_shopv2/plugin/app/core/mobile/member/cart/get_cart.php

$_W['action'] = $routes[0] . "." . $routes[1] . "." . $routes[2];
                
                $file = $root . $routes[0] . "/" . $routes[1] . "/" . $routes[2] . ".php";
                if (is_file($file)) {
                    $class = ucfirst($routes[2]);
                    
                    
                } elseif (is_dir($root . $routes[0] . "/" . $routes[1] . "/" . $routes[2])) {
                    $file = $root . $routes[0] . "/" . $routes[1] . "/" . $routes[2] . "/index.php";
                    $class = "Index";
                } else {
                    $method = $routes[2];
                    $file = $root . $routes[0] . "/" . $routes[1] . ".php";
                    if (is_file($file)) {
                        $class = ucfirst($routes[1]);
                    } elseif (is_dir($root . $routes[0] . "/" . $routes[1])) {
                        $file = $root . $routes[0] . "/" . $routes[1] . "/index.php";
                        $class = "Index";
                    }
                    $_W['action'] = $routes[0] . "." . $routes[1];
                }
                break;

r=member.cart.get_cart 对应 addons/ewei_shopv2/plugin/app/core/mobile/member/cart.php
文件里的get_cart方法

最终实例化类->方法

        $response = $instance->$method();
        
        if (!empty($response)) {
            echo $response;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值