Solved

CodeIgniter, dynamic menu and routes

Posted on 2015-01-01
9
643 Views
Last Modified: 2015-01-09
Hi all.
Well, so is my first question in 2015. I'm a CodeIgniter novice and I want to use it to develop a catalogue site for a client.
I have the database  with three tables: products, categories and subcategories. I build the menu using categories and subcategories and using the category_name and the category_name/category_name as slug. Nothing special, I guess.
the question is: what are the methods I have to configure route to accept these slugs? I wuld like some authomatism so when the client add a new category or a new subcategory, the new slug is automatically added to the routes.php.

Iguess I can write a small piece of code in the CMS, so when a new element is added the riutes.php file is updated, bit I wonder if there is some better way to do it.
Thanks to all in advance.
0
Comment
Question by:Marco Gasi
  • 5
  • 4
9 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40527396
Hi Marco,

I haven't done this in codeigniter (in cakePHP I used mod_rewrite) but I do know you should be able to use regular expressions in the route

e.g.

$route['product/(:any)'] = "catalog/product_lookup";

Where the url would be passed to your controller function 'product_lookup'.

See here for more info: https://ellislab.com/codeigniter/user-guide/general/routing.html
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 40527404
Hi Rob, thank you for quick reply.
I'll read the link, but I wish to know if this could work: I could use only one controller, one model and one view to display products; here the code could serve different contents accordingly to the parameters: is this the right way?
I could place only one row in the routes.php, let's say something like

$route[<-- regex here -->] = 'products/$1';

where $1 is parameters to pass to the controler->to the model to extract data from database.
But my links are www.example.com/Furnitures/Bedroom so I have to pass both parameters, Furnitures as category and Bedrooms as subcategory to serve a page which lists all products in that subcategory. So in routes.php I place only one $1 which represents all parameters, is it right?
I realize the question is a bit confusing, but I can't tell it better, at this moment
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40527410
Then you'll have to statically set the routes for the categories as im sure you'll have other non product pages such as

www.example.com/about
www.example.com/contact
...

OR

statically set the about, contact etc pages and a catch all at the bottom relating to your products but really you should have the following structure

www.example.com/products/cat/subcat
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 40527415
Yes, the idea is to set statically static pages such as home and contacts and set dinamically the other. In effect, I can have a structure liek the one yo said (products/cat/subcat). I have to take some time to read manuals about how to manage parameters...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40527699
See here for how they're passed to your controller functions: https://ellislab.com/codeigniter/user-guide/general/controllers.html#passinguri
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 40536295
Hi, Rob. I'm sorry for the delay, I haven't abandoned this question but I've been tto busy in other aspects of my project I had to complete more urgently. In the next day I'll come back to this: I have just to do some test to see if I understood the right way before to award points and to close this question.
Cheers
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40537613
By all means test it.  I'll be here if you have any questions :)
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 40540387
Well, it seems to work:

routes.php:
$route['products/(.+)?/(.+)?'] = 'Products/process_data/$1/$2';

Open in new window


The controller Products.php:
	public function process_data($data)
	{
                /*only to test*/
		echo "<pre>Process data data<br>";
		var_dump($data);
		echo "</pre>";
	}
	
	public function _remap($method, $params = array())
  {
    $map = array();
	foreach ($params as $p)
	{
		$map[] = $p;
	}
       if( $method[0] != '_' && method_exists( $this, $method ))
      {
          return $this->$method( $map );
      }
     show_404();
}

Open in new window


This way I should be able to process all requests. On to the next and thank you for your fruitful help!
Cheers
0
 
LVL 31

Author Closing Comment

by:Marco Gasi
ID: 40540390
Thank you again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now