drudfe0446838 2015-12-04 19:30 采纳率: 0%
浏览 11

如何在路由系统中制作自定义404页面

I want to make a 404 error page on this routing. How can this be done?

index.php

<?php

    include 'route.php';
    include 'control/about.php';
    include 'control/home.php';
    include 'control/contact.php';

    $route = new route();

    $route->add('/', function(){
        echo 'Hello, this is home pageeees';
    });
    $route->add('/about', 'about');
    $route->add('/contact', 'contact');

    echo '<pre>';
    print_r($route);

    $route->submit();

?>

route.php

<?php


class route 
{   

    private $_uri = array();
    private $_method = array();

    /**
    *Builds a collection of internal URL's to look for
    *@parameter type $uri
    */

    public function add($uri, $method = null)
    {
        $this->_uri[] = '/' . trim($uri, '/');

        if($method != null){
            $this->_method[] = $method;
        }
    }

    /**
    *Makes the thing run! 
    */

    public function submit()
    {

        $uriGetParam = isset($_GET['uri'])? '/' . $_GET['uri'] : '/';

        foreach ($this->_uri as $key => $value)
        {   
            if(preg_match("#^$value$#",$uriGetParam))
            {
                if(is_string($this->_method[$key]))
                {   
                    $useMethod = $this->_method[$key];
                    new $useMethod();
                }
                else
                {
                    call_user_func($this->_method[$key]);
                }
            }
        }
    }


}



?>
  • 写回答

1条回答 默认 最新

  • dongnaota6386 2015-12-04 19:57
    关注

    The regex in mind I would just add $route->add("/.+", function()...); after all your other routes.

    Proof: https://3v4l.org/nk5PZ


    But you could also program some logic which evaluates custom 404-Pages when not being able to find a correspondig route.

    For example:

    private $_notFound;
    
    public function submit() {
        $uriGetParam = isset($_GET['uri'])? '/' . $_GET['uri'] : '/';
        $matched = false;
        foreach ($this->_uri as $key => $value) {   
            if(preg_match("#^$value$#", $uriGetParam)) {
                if(is_string($this->_method[$key])) {   
                    $useMethod = $this->_method[$key];
                    new $useMethod();
                } else {
                    call_user_func($this->_method[$key]);
                }
                $matched = true;
            }
        }
        if(!$matched) {
            if(isset($this->_notFound)) {
                if(is_string($this->_notFound)) {
                    $action = $this->_notFound;
                    new $action();
                } else {
                    call_user_func($this->_notFound);
                }
            }
        }
    }
    
    public function notFound($callback) {
        $this->_notFound = $callback;
    }
    

    Then you'll have to add the 404-action by $route->notFound(function... or "class");

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题