框架的搭建之路由类

首先在我们的根目录下创建.htaccess文件:

#开启重写引擎
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f  [NC]
RewriteRule ^(.*)$  index.php/$1  [L]

在修改core\imooc.php中的run方法:

<?php

namespace core;

class imooc
{
	public static $classMap = array();
	public $assign;

	//基类库
	static public function run()
	{
		$route = new \core\lib\route();
        }
}

最后完成route.php:

<?php
namespace core\lib;
use core\lib\conf;

class route
{
    public $ctrl;
    public $action;
    public function __construct()
    {
        /**
         * 1.隐藏index.php
         * 2.获取URL参数部分
         * 3.返回对用的控制器和方法
         */
        if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URL'] != '/') {
            $path = $_SERVER['REQUEST_URI'];
            $patharr = explode('/',trim($path, '/'));

            if (isset($patharr[0])) {
                $this->ctrl = $patharr[0];
            }
            unset($patharr[0]);
            if (isset($patharr[1])) {
                $this->action = $patharr[1];
                unset($patharr[1]);
            } else {
                $this->action = 'index';
            }
            //把url多余的部分转换成 GET
            $count = count($patharr) + 2;
            $i = 2;
            while ($i < $count) {
                if (isset($patharr[$i + 1])) {
                    $_GET[$patharr[$i] = $patharr[$i + 1]];
                }
                $i = $i +2;
            }
        } else {
            $this->ctrl = 'index';
            $this->action = 'index';
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值