Jeff Davidson
Jeff Davidson

Reputation: 1929

URI Routing for codeigniter

I'm trying to figure out how I should do this. The following controller is for a bio page for each wrestler. Here's an example.

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

Now if you notice there's three links Biography, Wrestling, Appearances.

One question I have is should all three be different functions inside this controller?

If the answer is yes are the links actually correct on the page link?

<?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 */

EDIT: I'm really concerned with the biography page link when I'm on the bio page like the link above.

Here's what I have currently for my route: $route['bio/(:any)'] = "bio/index/$1";

Upvotes: 0

Views: 195

Answers (2)

Shomz
Shomz

Reputation: 37701

It would be the best to have separate controllers for each of the 3 links.

But if you don't want to, and still want the links /appearances/whatever, here's the routing you need to keep it all within the Bio controller:

UPDATE - this is still a bad approach, but it should do.

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";
}

UPDATE 2: you got me confused, but the first solution HAS to work, even the order doesn't matter:

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

bio/kid goes to bio/index/kid

wrestling/kid goes to bio/wrestling/kid

appearances/kid goes to bio/appearances/kid

Upvotes: 1

Jose Vega
Jose Vega

Reputation: 10258

You currently have this setup:

The functions

function index($wrestlerName = null){ }
function wrestling($wrestlerName = null){ }
function appearances($wrestlerName = null){ }

The links

bio/kid-wonder
bio/wrestling/kid-wonder
bio/appearances/kid-wonder

If you wanted to have the wrestling/kid-wonder and appearances/kid-wonder without the bio at the beginning of the url, you are going to need to create new controllers for wrestling and appearances.

class wrestler extends CI_Controller {
 function index($wrestlerId = NULL){
   if($wrestlerId != NULL){

   }
 }
}

class appearances extends CI_Controller {
 function index($wrestlerId = NULL){
   if($wrestlerId != NULL){

   }
 }
}

Upvotes: 1

Related Questions