首页 > 解决方案 > PHP MVC - 使用 URL 格式打开特定选项卡

问题描述

我正在使用 PHP MVC,并且我已经编写了下面的代码来创建 URL 和加载核心控制器。我的 URL 格式是这样的 - “/controller/method/params”。现在,如果我尝试通过在 URL 中传递选项卡的#id 来打开特定的导航选项卡,我会遇到一些问题,它会忽略该部分。我想如果我传递一个选项卡的 ID,那么它应该打开该活动选项卡。任何帮助将不胜感激。

 <?php
  /*
   * App Core Class
   * Creates URL & loads core controller
   * URL FORMAT - /controller/method/params
   */

 class Core
 {  
    protected $currentController = 'Pages';
    protected $currentMethod= 'index';
    protected $params = [];

    public function __construct()
     {
       //print_r($this->getUrl());
       $url = $this->getUrl();

       if (empty($url) || !in_array("user_images", $url)){


       //Look in controller for first value
       if (file_exists('../app/controllers/'. ucwords($url[0]) . '.php' )) {
            //if exists, set as controller
            $this->currentController = ucwords($url[0]);
            //Unset 0 Index
            unset($url[0]);

          }



          require_once '../app/controllers/' . $this->currentController . '.php';
         
          //instantiate controller class
          $this->currentController = new $this->currentController;

          //check for second part of the url

          if (isset($url[1])) {
            if (method_exists($this->currentController, $url[1])) {
                $this->currentMethod = $url[1];
                unset($url[1]);
            }
          }

          //echo $this->currentMethod;
          // Get Params
          $this->params = $url ? array_values($url) : [];

          //call a callback with array of params

          call_user_func_array([$this->currentController, $this->currentMethod],
            $this->params);

       }
       



     }

    public function getUrl(){
        if (isset($_GET['url'])) {
            $url = rtrim($_GET['url'],'/');
            $url = filter_var($url, FILTER_SANITIZE_URL);
            $url = explode('/',$url);
            return $url;
        }
    }
     
     
 }
  

我可以在 getUrl 方法中进行哪些更改? 在此处输入图像描述

标签: phpjquerymodel-view-controller

解决方案


仅在#客户端(浏览器)上用作页面中元素的锚点。此外,它可以在 JavaScript 上下文中使用以处理选项卡等。

它不在服务器端处理,您应该只发送参数:?tab=id

您可以访问tab服务器端脚本中的参数。


推荐阅读