【问题标题】:PHP MVC - Open a specific Tab using URL FormatPHP MVC - 使用 URL 格式打开特定选项卡
【发布时间】:2020-06-21 04:43:46
【问题描述】:

我正在使用 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 方法中做哪些更改?

【问题讨论】:

    标签: php jquery model-view-controller


    【解决方案1】:

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

    服务器端不处理,只发送参数:?tab=id

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

    【讨论】:

    • 嗨 Maarten,我怎样才能更改我的 getURL 方法以接受“?”还有吗?
    • tab 参数自动进入$_GET。所以从那里读吧。
    • @Marteen id 基本上是特定选项卡的引导导航选项卡 ID。我想通过在 URL 中传递 id 来加载它。现在请您看看我的代码,让我知道应该在哪里进行此更改?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2018-04-29
    • 2013-02-01
    相关资源
    最近更新 更多