【问题标题】:Ignoring Codeigniter's _remap for certain URLs?忽略某些 URL 的 Codeigniter 的 _remap?
【发布时间】:2011-09-06 05:44:02
【问题描述】:

我使用 Codeigniter 的 _remap 函数将所有 url 重新映射到 www.website.com。这适用于 99% 的情况,但我希望它忽略某些 URL(特别是 /admin、/contact、/submit、/top、/browse)。这些 URL 包含我想要显示的内容。

我将如何实现这一目标?

public function _remap($urlname)
{
    if($urlname == "index") {
        // If this is the index, redirect to the most recent startup
        redirect("s/".$this->Startup_model->getMostRecent(), 'refresh');
    } else {
        // If they didn't go to the index, find the startup they were after
        $id = $this->Startup_model->matchName($urlname);

        // Check to see if the name they entered was a real startup, if not redirect to missing page
        if($id == null) {
            $data['urlname'] = $urlname;
            $this->load->view('header/header');
            $this->load->view('content/missing', $data);
            $this->load->view('footer/footer');
        } else {
            // They got the name right!  Load the page...
            $data['values'] = $this->Startup_model->buildStartup($id);

            // If there was no next startup avaiable, pass that info through
            $next = $this->Startup_model->findNext($id);
            if($next == null) {
                $next = '343z61v';
            }

            // If there was no previous startup avaiable, pass that info through
            $previous = $this->Startup_model->findPrevious($id);
            if($previous == null) {
                $previous = '343z61v';
            }

            $data['next'] = $next;  
            $data['previous'] = $previous;

            $this->load->view('header/header');
            $this->load->view('content/startup', $data);
            $this->load->view('footer/footer');
        }
    }
}

【问题讨论】:

  • 你可以把你的 _remap 函数放进去吗?

标签: codeigniter routes


【解决方案1】:

试试这个:

function _remap($urlname, $params = array())
{
    if(is_callable(array($this, $urlname))){ //If the function exists, is called, if not using the usual
        return call_user_func_array(array(&$this, $urlname), $params);
    }elseif($urlname == "index") {
            // If this is the index, redirect to the most recent startup
            redirect("s/".$this->Startup_model->getMostRecent(), 'refresh');
    } else {
        // If they didn't go to the index, find the startup they were after
        $id = $this->Startup_model->matchName($urlname);

        // Check to see if the name they entered was a real startup, if not redirect to missing page
        if($id == null) {
            $data['urlname'] = $urlname;
            $this->load->view('header/header');
            $this->load->view('content/missing', $data);
            $this->load->view('footer/footer');
        } else {
            // They got the name right!  Load the page...
            $data['values'] = $this->Startup_model->buildStartup($id);

            // If there was no next startup avaiable, pass that info through
            $next = $this->Startup_model->findNext($id);
            if($next == null) {
                $next = '343z61v';
            }

            // If there was no previous startup avaiable, pass that info through
            $previous = $this->Startup_model->findPrevious($id);
            if($previous == null) {
                $previous = '343z61v';
            }

            $data['next'] = $next;  
            $data['previous'] = $previous;

            $this->load->view('header/header');
            $this->load->view('content/startup', $data);
            $this->load->view('footer/footer');
        }
    }
}

注意: 函数 index() 不应该存在

【讨论】:

    【解决方案2】:

    所以您问的是如何选择性地不重新映射某些网址?如果是这样,请将其添加到您的 if 语句中的 else 之前:

    else if (in_array($urlname, array('admin', 'contact', 'etc')))
    {
        $this->$urlname;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2012-03-09
      • 2011-04-06
      相关资源
      最近更新 更多