• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Place several elements in the router

#1
Hi eveybody

It's my first post on the forum and it's why i well present myself. Firstly, I'm French, so please excuse my grammar and my language mystakes Smile. I'm coming from the South of France and I want to use CodeIgniter for a big project. Actually it's a secret project for french people.

I ha a little question about the routing in codeigniter. I am creating a forum and i want to pass all my parameters in the URL just like that :

http://base.com/forum/category-name/foru...topic-name

And I would for :
  • forum/ -> list all the forums
  • forum/category-name -> list all the forum of this category
  • forum/category-name/forum-name -> list all the topics of this forum
  • forum/category-name/forum-name/topic-name -> all posts of this topic


And I would to do it automatically with the router. But this code doesn't run :

PHP Code:
$route['forum'] = 'forum/index';
$route['forum/(.+)'] = 'forum/index/$1';
$route['forum/(:any)/(:any)'] = 'forum/index/$2';
$route['forum/(:any)/(:any)/(:any)'] = 'index/$1/$2/$3'

with theses functions in the Forum Controller :

PHP Code:
/**
     * Index avec toutes les catégories
     */
    
public function index()
    {
        
$this->cat();
    }

    
/**
     * Index avec une catégorie spécifiée
     *
     * @param null $id
     */
    
public function cat($id NULL)
    {
        if( 
$id === NULL )
        {
            
$title 'Forums';
        }
        else
        {
            
$title 'Une catégorie';
        }

        
$this->data array_merge($this->data, [
            
'title'         =>  $title,
            
'navigation'    =>  $this->my_site->page_navigation($title),
            
'flash'         =>  $this->my_site->page_flash(),
            
'forums'        =>  $this->forum_model->get_forums($id),
        ]);

        
$this->twig->display('forums/forums_index.twig'$this->data);
    }

public function 
forum($slug)
    {
        
$forum $this->forum_model->get_forum_id($slug);

        
$this->data array_merge($this->data, [
            
'title'         =>  $forum['f_name'],
            
'navigation'    =>  $this->my_site->page_navigation($forum['f_name']),
            
'flash'         =>  $this->my_site->page_flash(),
            
'topics'        =>  $this->forum_model->get_topics($foru['f_id']),
        ]);

        
$this->twig->display('forums/forums_topics.twig'$this->data);
    } 

I don't know if my method is right or if I have an error with my code but I would learn the best practice to do that Smile

Thank you very much Smile
Reply

#2
Would be simpler with a url like /forum/cat/category-name/forum-name/topic-name

function cat ($category = "all", $forum = "all", $topic = "all")

http://www.codeigniter.com/user_guide/ge...passinguri
Reply

#3
Exactly yes Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.