opencart url美化,增加html后缀

步骤一:后台开启优化

opencart url美化,增加html后缀

 

 

 

步骤二:nginx配置伪静态

location / {
    try_files $uri @opencart;
}

location @opencart {
    rewrite ^/sitemap.xml$ /index.php?route=extension/feed/google_sitemap last;
    rewrite ^/googlebase.xml$ /index.php?route=extension/feed/google_base last;
    rewrite ^/payment_callback/(.*) /index.php?route=extension/payment/$1/callback last;
    rewrite ^/callback/(.*) /index.php?route=extension/module/social/login&provider=$1 last;
    rewrite ^/system/download/(.*) index.php?route=error/not_found last;
    rewrite ^/blog$ /index.php?route=panda/blog last;
    rewrite ^/(.+)$ /index.php?_route_=$1 last;
}

location /admin/ {
    index index.php;
}

location = /robots.txt {
    allow all;
}

location ~* (\.(js|css|png|jpg|jpeg|gif|ico|otf|eot|svg|ttf|woff|woff2))$ {
    expires max;
}

location ~* (\.(tpl|ini|twig|log))$ {
    deny all;
}

伪静态来源于,opencart根目录下

步骤三:开启html后缀

3.1 url自动带上html

D:\wwwroot\opencart.net\system\library\url.php第77行

public function link($route, $args = '', $auto_admin_token = true) {
        $url = $this->url . 'index.php?route=' . (string)$route;

        // Add user_token to admin link if it's not passed in
        if ($auto_admin_token && is_admin() && $user_token = array_get(session()->data, 'user_token')) {
            if (is_array($args) && !in_array('user_token', $args)) {
                $args['user_token'] = $user_token;
            } else if (!str_contains($args, 'user_token')) {
                $args .= '&user_token=' . $user_token;
            }
        }

        if ($args) {
            if (is_array($args)) {
                $url .= '&' . http_build_query($args);
            } else {
                $url .= str_replace('&', '&', '&' . ltrim($args, '&'));
            }
        }

        foreach ($this->rewrite as $rewrite) {
            $url = $rewrite->rewrite($url);
        }

        if ($route == 'common/home') {
            $url = str_replace('index.php?route=common/home&', '?', $url);
            $url = str_replace('index.php?route=common/home', '', $url);
        }
        # $url 改为 $url.'.html'
        return $url.'.html';
    }

3.2路由过滤掉html(如果不过滤,会指向错误)

D:\wwwroot\opencart.net\system\engine\action.php 

public function execute($registry, array $args = array()) {
        // Stop any magical methods being called
        if (substr($this->method, 0, 2) == '__') {
            return new \Exception('Error: Calls to magic methods are not allowed!');
        }

        $file = DIR_APPLICATION . 'controller/' . $this->route . '.php';
        $class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $this->route);


        /**
         * 连接地址增加后缀 html Author:snails time:2022-01-06
         * */
        # 连接view-source:http://opencart.net/software/imac.html,对应的是_route_
        $request = $registry->get('request');
        if(isset($request->request['_route_']) && !empty($request->request['_route_'])){
            $r = str_replace('.html','',$request->request['_route_']);  // 替换html
            $request->get['_route_'] = $r;
        }
        # 链接http://opencart.net/index.php?route=information/contact.html,对应的是route
        if(isset($request->request['route'])  && !empty($request->request['route'])){
            $r = str_replace('.html','',$request->request['route']);
            $request->get['route'] = $r;
        }

        // 重新赋值给get
        $registry->set('request',$request);
        /* End */


        // Initialize the class
        if (is_file($file)) {
            include_once($file);

            $controller = new $class($registry);
        } else {
            return new \Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!');
        }

        $reflection = new ReflectionClass($class);

        if ($reflection->hasMethod($this->method) && $reflection->getMethod($this->method)->getNumberOfRequiredParameters() <= count($args)) {
            return call_user_func_array(array($controller, $this->method), $args);
        } else {
            return new \Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!');
        }
    }

 

 

注意事项:

控制路由指向到php文件,到方法的关键位置在get

$request = $registry->get('request');
print_r($request);die;

这里的route有两种形式:route(常规连接),_route_(伪静态连接)

Request Object
(
    [get] => Array
        (
            [route] => information/contact.html  #路由转发最核心位置,如果不过滤html转发就报错
        )

    [post] => Array
        (
        )

    [cookie] => Array
        (
            [folder_language] => zh-cn
            [currency] => CNY
            [language] => en-gb
            [OCSESSID] => b66056cef14556ff2eb1a53db7
            [PHPSESSID] => fg4r8u27hevv2nur7gqno9nuob
            [__atuvc] => 16|1
            [__atuvs] => 61d6c7c16ac98e8b006
        )

    [files] => Array
        (
        )

    [server] => Array
        (
            )

    [request] => Array
        (
            [route] => information/contact.html
        )

)

 

 

3.1路由过滤掉html(如果不过滤,会指向错误)

上一篇:一个文件上传界面(带有进度条)


下一篇:Dubbo 实现一个Route Factory(用于灰度发布)