ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

opencart url美化,增加html后缀

2022-01-06 19:32:26  阅读:165  来源: 互联网

标签:index url opencart request html php route


步骤一:后台开启优化

 

 

 

步骤二: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(如果不过滤,会指向错误)

标签:index,url,opencart,request,html,php,route
来源: https://www.cnblogs.com/wesky/p/15772475.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有