忽略Codeigniter的某些网址的_remap?

时间:2011-09-05 19:52:29

标签: codeigniter routes

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

我将如何实现这一目标?

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');
        }
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个:

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()不应该存在

答案 1 :(得分:1)

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

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