codeigniter的URI路由

时间:2011-11-22 16:38:12

标签: php codeigniter

我试图弄明白我应该怎么做。以下控制器用于每个摔跤手的生物页面。这是一个例子。

http://kansasoutlawwrestling.com/bio/kid-wonder

现在,如果你注意到传记,摔跤,外表三个链接。

我遇到的一个问题是,这三个控制器内的所有三个功能都应该不同吗?

如果答案是肯定的,那么链接在页面链接上是否正确?

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bio extends CI_Controller
{

function index($character = "jfkdlsjl")
{

    //Config Defaults Start
    $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg
    $cssPageAddons = '';//If you have extra CSS for this view append it here
    $jsPageAddons = '';//If you have extra JS for this view append it here
    $metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's
    $siteTitle = '';//alter only if you need something other than the default for this view.
    //Config Defaults Start


    //examples of how to use the message box system (css not included).
    //$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...');

    /**********************************************************Your Coding Logic Here, Start*/


    $activeTemplate = $this->sitemodel->getTemplate();
    $footerLinks = $this->sitemodel->getFooterNav();
    $bodyContent = "bio";//which view file
    $bodyType = "main";//type of template
    $this->data['activeTemplate'] = $activeTemplate;
    $this->data['footerLinks']= $footerLinks;
    $this->load->model('biomodel');
    if($character !== "jfkdlsjl")
    {
        if((!empty($character))||(!isset($character))||(trim($character) !== '')||($character !== NULL))
        {
            $bioArray = $this->biomodel->getCharacterBio($character);
            if ($bioArray == "empty")
            {
                $this->data['bioArray']= array();
            }
            else
            {
                if (($bioArray[0]->characters_statuses_id == 2)||($bioArray[0]->characters_statuses_id == 3)||($bioArray[0]->characters_statuses_id == 5))
                {
                    $this->data['bioArray']= array(); 
                }
                else
                {
                    $this->data['bioArray']= $bioArray;
                    $bioPagesArray = $this->biomodel->getBioPages();
                    $alliesArray = $this->biomodel->getCharacterAllies($bioArray[0]->id);
                    $rivalsArray = $this->biomodel->getCharacterRivals($bioArray[0]->id);
                    $quotesArray = $this->biomodel->getCharacterQuotes($bioArray[0]->id);
                    $this->data['bioPagesArray']= $bioPagesArray;
                    $this->data['alliesArray']= $alliesArray;
                    $this->data['rivalsArray']= $rivalsArray;
                    $this->data['quotesArray']= $quotesArray;
                }
            }
        }
    }

    /***********************************************************Your Coding Logic Here, End*/

    //Double checks if any default variables have been changed, Start.
    //If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.
    if(count($msgBoxMsgs) !== 0)
    {
        $msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs));
    }
    else
    {
        $msgBoxes = array('display' => 'none');
    }

    if($siteTitle == '')
    {
        $siteTitle = $this->metatags->SiteTitle(); //reads
    }

    //Double checks if any default variables have been changed, End.

    $this->data['msgBoxes'] = $msgBoxes;
    $this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view.
    $this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view.
    $this->data['metaAddons'] = $metaAddons;//if there is any addictional meta data to add from the above variable this will send it to the view.
    $this->data['pageMetaTags'] = $this->metatags->MetaTags();//defaults can be changed via models/metatags.php
    $this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php
    $this->data['bodyType'] = $bodyType;
    $this->data['bodyContent'] = $bodyContent;
    $this->load->view($activeTemplate[0]->short_name.'/index', $this->data);

}
}

/* End of file bio.php */
/* Location: ./application/controllers/bio.php */
编辑:当我在上面的链接上的生物页面上时,我真的很关心传记页面链接。

以下是我目前的路线:$ route [&#39; bio /(:any)&#39;] =&#34; bio / index / $ 1&#34 ;; < / p>

1 个答案:

答案 0 :(得分:1)

最好为3个链接中的每个链接分别使用控制器。

但是如果您不想,并且仍然想要链接/appearances/whatever,那么您需要将所有链接保存在Bio控制器中:

UPDATE - 这仍然是一个糟糕的方法,但应该这样做。

if ($this->uri->segment(1) == 'bio') {
    $route['bio/(:any)'] = "bio/index/$1";
} else {
    $route['wrestling/(:any)'] = "bio/wrestling/$1";
    $route['appearances/(:any)'] = "bio/appearances/$1";
}

<击>

更新2:你让我感到困惑,但第一个解决方案仍然有效,即使订单无关紧要:

$route['bio/(:any)'] = "bio/index/$1";
$route['wrestling/(:any)'] = "bio/wrestling/$1";
$route['appearances/(:any)'] = "bio/appearances/$1"; 
  

bio/kid转到bio/index/kid

     

wrestling/kid转到bio/wrestling/kid

     

appearances/kid转到bio/appearances/kid