How to convert CodeIgniter urls into user friendly ones? - codeigniter

I've explored lot of questions and articles regarding this but I can't find how to get this done.
I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)
Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)
My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.
Thanks for reading.
Edit:
Route.php
$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;
This is how I'm building the anchor links (view_file->index.php, called from Index method):
<?php
foreach model(in the table)
echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
end foreach
?>

I can get the desired URLs with following code in route.php. Only problem is I'm not able to make the 'urlController/urlMethod' return a value in the function which can be assigned to $result variable.
$route['(:any)'] = function ($1)
{
$result = 'urlController/urlMethod/'.$1;
return $result;
};
I'm not sure how to do this. Can someone suggest how I should call 'urlController/urlMethod'?

You could achieve it with CodeIgniter URI Routing. Considering
localhost/sitename/galaxy-tab-s3
maps to
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
And, model id i.e 4, in this case, is static with respect to galaxy tab s3, as you have not mentioned any such Id in the simplified URL.
My understanding is with every URL localhost/sitename/iphone-6, you need three details about the string 'iphone-6'. i.e. type of product, model-slug, model id. One way could be write something like
$route['sitename/(:any)'] = 'routingController/commonRoutingMethod/$1';
Here, create a new routingController and write some logic into commonRoutingMethod() method, which takes the string like iphone-6 and fetches its all three details i.e. product type, model id etc. And then redirects by building the exact URL using
header('Location: http://localhost/sitename/specs/display/$productType/$modelSlug/$modelId/');
NOTE : There could be more forward ways just using regex match in routes.php, given that you create diffrentiated structure of the string, based on product type and model id e.g p_iphone-6_1 or t_galaxy-tab-s3_4.

Please use below routing code, to achieve it.
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
$route['(:any)'] = 'specs/display/$1/$1/$1';
Let me know if you need any help.

Related

Codeigniter route products, categories and sub-categories

I am using Codeigniter for my project.
I need to get URL structure like this:
Main product page
example.com/language-prefix/products/
Select products by category
example.com/language-prefix/products/category
Select products by category AND sub-category:
example.com/language-prefix/products/category/sub-category
Select specifict product under category AND sub-category:
example.com/language-prefix/products/category/sub-category/product-name
OR only under category
example.com/language-prefix/products/category/product-name
Question is - what would be a good solution for this? Because problem starts here:
example.com/language-prefix/products/category/what-ever
what-ever can be a product or a sub-category and how to decide - what data and view should be returned?
In my DB table structure I have many-to-many relationships between products and categories. It means, than one product can be assigned to many categories. Every category has a self refferecing foreign key (parent).
Maybe I need to get some restrictions for category adding for products? Or specifing a main category or what?
I have couple of ideas:
Keep every route for category/sub-category and product in DB for example:
/products/watches/for-men/
/products/watches/for-men/rolex-abc-whatever-product
And so on, and so on. But this, I have feeling could be very slow.
Make a simple route to products controller -> method view(), and in the method go trough all passed segments and when it comes to
/products/category/what-ever
then first check if there exists such product, if true, then return product_view and data, if not, check if there is such sub-category and if there is, then return the product grid or return 404 if there is not.
I assume a simple solution could be just keep all categories and sub-categories after /products/ and add category-id like
example.com/language-prefix/products/1-watches
example.com/language-prefix/products/2-for-men
But I hope there is better solution for this.
Also I cant figure out this:
example.com/language-prefix/products/category/sub-category/product-name
I need to return product with name product-name AND check if it is under those two categories so URL for example:
example.com/language-prefix/products/hello-kity/bla-bla/product-name
would NOT return that product.
Any better/other solutions?
"Keep every route for category/sub-category and product in DB"
the idea above won't work well in CI and you'll probably break the way CI routing works. remember that the route points you to a controller/function. only when you get there can you start calling the db... so this idea will be weird since you gotta' call the db first to match the routes.
"Make a simple route to products controller -> method view(), and in the method go trough all passed segments and when it comes to /products/category/what-ever"
the idea above is better, you could have a route like this $route['[a-z]{2}/products/(:any)'] = "product/index/$1"; so http://www.example.com/sg/products/gems/ruby and http://www.example.com/sg/products/gems/red/ruby will both go to the product controller's index function
segment(4) can be either "red" which is a sub-category or "ruby" which is a product. segment(3) is always going to be a category. segment(5) is either empty or a product. you must now simply decide if products or sub-categories that precedence, better still handle within your code logic that they never will collide. i.e. product and category/sub-category names cannot be duplicated.
public function test($category = NULL, $sub_category_or_product = NULL, $product = NULL)
{
echo $category.'<br />'; // segment(3) is always your category
if ($product)
{
echo $sub_category_or_product.'<br />'; // segment(4) is a sub-category
echo $product.'<br />'; // segment(5) is a product
}
else
{
echo $sub_category_or_product.'<br />'; // it's a sub-category or product
// call the db and test whether its a sub-category
// then test if its a product... or the other way around
}
}
of cuz this is just one of the many possible ways to solve it...
however if you don't have to stick with that URL structure...
i would suggest you put products/categories/sub-categories under the "/products/" namespace in the URL for "listing view". and put products under the "/p/something" namespace for the details view
www.example.com/p/ruby // product details
www.example.com/products // product listing
www.example.com/products/gems // product listing
www.example.com/products/gems/red // product listing
for example see:
http://www.beautylish.com/products
http://www.beautylish.com/p/mac-mineralize-skinfinish-natural
this way you won't have to worry about categories/sub-categories names clashing with product names
people probably want to put the cats and sub cats in the product url for seo... but if you check beautylish they got very damn good seo without choking up the url and simply putting only the product name, it's also shorter and more readable.

How to work with multiple levels of subcategories in Codeigniter?

What is the smartest solution to work with multiple levels of subcategories in Codeigniter? I want to make a web site for travel agency with travel offers, and the structure will look something like this:
- Cat1
- Cat2
--- Subcat1
--- Subcat2
------ Subcat1
--------- Subcat1
--------- Subcat2
------------ Subcat1
------------ Subcat2
--------------- offer1
--------------- offer2
--------------- offer3
--------------- offerN
------------ Subcat3
--------- SubcatN
------ Subcat2
------ SubcatN
--- Subcat3
--- SubcatN
- Cat3
--- offer1
--- offer2
--- offer3
--- offerN
- CatN
This is an example of the nested categories. Every (sub)category should have: Main picture, Description, and list of subcategories OR list of the offers.
I have some idea to do that with a table called Categories (ID, cat_name, parent) and if the parent is 0, that it is top level category, if the parent is some int, than that value is the ID of the parent category...
But I don't have idea how to solve the routing with the controller? I know how to do that with only 2 levels (main category and subcategories) but they are manually added to the routes. I don't know how to do that dynamically so there can be added more categories and subcategories in future through the admin panel.
Of course I will have another table for my offers, I know that, and I don’t have problems with that. The problem is how the controller(s) structure will look like?
For my present project with only 1 level of subcategory and without option to dynamically add subcategories, here is what I have in routes.php
$route[‘cat1’] = “category”;
$route[‘cat1/(:any)’] = “category/subcategory”;
$route[‘cat2’] = “category”;
$route[‘cat2/(:any)’] = “category/subcategory”;
$route[‘cat3’] = “category”;
$route[‘cat3/(:any)’] = “category/subcategory”;
$route[‘cat4’] = “category”;
$route[‘cat4/(:any)’] = “category/subcategory”;
$route[‘cat5’] = “category”;
In other words, cat1,2,3,4,5 are real names of the categories, and :any represents all of their subcategories and when the visitors types example.com/pc_news than it is redirected to the controller named category and it’s index function. if he types example.com/pc_news/hardware than he hell be redirected to the method subcategory of the controller category.
If I want to add more categories and subcategories, I will do that manually at the routes, but if I want to add subsubcategory (example.com/pc_news/hardware/something) I don’t know how to achieve that.
On the new project I want to do, I will have something like:
example.com/summer/turkey/airplane/alanya/hotels # and it will list all the offers
example.com/summer/turkey/airplane/alanya/apartments
example.com/summer/turkey/bus/kushadasi
example.com/winter/bus/france/hotels
example.com/winter/airplane/france/hotels
Anybody with some idea how to do this?
As for the routing, i would just use the routes config to send everything to a method where you can implement any logic you want, if you have methods you still want to access inside that controller, you have to list them before the "catch any" line something like this:
config/routes.php
$route['products/save/(.*)'] = 'products/save/$1';
$route['products/(.*)'] = 'products/index/$1';
Once you have this set up, you will get every parameter inside the Products::index method:
controllers/products.php
class Products extends CI_Controller {
public function index() {
$args = func_get_args();
// now $args holds the url segments
$categories = array();
foreach ($args as $cat_name) {
$categories[] = $this->db
->from('categories')
->where('cat_name', $cat_name)
->get()
->row();
}
}
}
As for storing the categories, if you only have the parent_id in rows you will have to loop or use a bunch of joins to get every category. If you have only 3-4 levels it probably not really a problem, also probably won't change too often so it could be easily cached. If you want a more efficient solution look into the nested set models.
Update (I've expanded the question from the linked forum)
You can use the special 404_override key (at the bottom of the page) to make every request go to a specific controller's index method. Don't forget that you will need to handle 404 url's inside the Products controller!:
config/routes.php
$route['404_override'] = 'products';
The original answer can be expanded to do this, whitelist everything you don't want to send to the Products controller (every other controller's name basically) and have a catch-all route at the end to send other's to the Products controller. 404 url's still need to be handled.
config/routes.php
// place any specific rules on top
$route['about'] = 'main/about';
// have whitelist existing controllers, make them go to their original place like it normaly would
$route['(controller1|controller2|controller3)(/?.*)'] = '$1$2';
$route['(:any)'] = 'products/index/$1'; // the last "catch all" row goes here

Magento - get results view HTML for a collection of products

I get a list of magento ids from a web service. I load these into and array $product_ids, so I have something like this:
Array
(
[0] => 1965
[1] => 3371
[2] => 1052
)
I can then make this into a collection:
$collection = Mage::getModel('catalog/product')->getCollection()
->addIdFilter($product_ids);
Using my Magento inspector, I've seen that the category pages use the class Mage_Catalog_Block_Product_List to display lists of products. I'd like to do something similar in my class. I've tried loading:
$ProductList = new Mage_Catalog_Block_Product_List();
$ProductList->setCollection($collection);
And then I've tried to load the HTML of the results as follows:
$CollectionHTML = $ProductList->_toHtml();
But $CollectionHTML is empty.
How would I get the HTML of what you see in the list view (i.e. the generated output of frontend/base/default/template/catalog/product/list.phtml, but given my collection)?
Making the code work the right way is much more easier in Magento than trying to work with ugly legacy code. I would gladly help you make the code the proper way when you have specific questions. Also, in the longterm, technical debt is gonna cost alot more.
Anyway, back to your issue.
In Magento block are not instantiated like in any app $myvar = new className ... almost never. This tutorial can help you understand better Magento's layout and blocks.
But if you want to create a block a way to do it is:
$block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list')
Now related to your product collection you should check how Mage_Catalog_Block_Product_List::_getProductCollection actually works, because it uses the layered navigation, not a simple product collection.
Further, assuming that at least you are using a Magento controller and you are within a function, the following code will display the first page of products for a specified category:
//$category_id needs to be set
$layout = Mage::getSingleton('core/layout');
$toolbar = $layout->createBlock('catalog/product_list_toolbar');
$block = $layout->createBlock('catalog/product_list');
$block->setChild('toolbar', $toolbar);
$block->setCategoryId($category_id);
$block->setTemplate('catalog/product/list.phtml');
$collection = $block->getLoadedProductCollection();
$toolbar->setCollection($collection);
//render block object
echo $block->renderView();
Displaying specific ids:
you use root category id for $category_id variable (also make sure that display root category is set (or another category id that contains your product ids)
you can hook into catalog_block_product_list_collection event to add your ID Filter to the collection (this is called in _beforeToHtml function)
But, all this construction is not solid and there are still some points that require attention (other child blocks, filters and so on)

Get Joomla! category from an URL

How can I read the section a certain URI belongs to?
I want to enhance the mod_breadcrumb to put section and category into the HTML. JApplication->getPathway() returns a JPathway which basically holds an assiciative array combining a name and an URL (as $list[]->name and $list[]->link). I think, it should be possible to get the section and category from a link, but don't know how.
A starting point could be the parsing into JURI-Object, but from there I don't know how get get further. Any ideas?
Pretty straight forward...
I assume you want to add category and section for the article and not your custom component.
Check if requested current URL is for article. If it is for article you know the article ID, use this article Id to go database and get catid from #__content, Use this cat_id to go to #__categories and get section (this is section id), go to #__sections to get the proper section name. All this can be done in 1 sql statement.
$breadcrumbs =& JFactory::getApplication()->getPathway();
$breadcrumbs->addItem("SECTION_NAME", JRoute::_("index.php?option=com_content&view=section&id=SECTION_ID"));
$breadcrumbs->addItem("CATEGOY_NAME", JRoute::_("index.php?option=com_content&view=category&id=CATEGORY_ID"));
$breadcrumbs->addItem("Article");
Alternatively, if you know the URL from the breadcrumb item. You can parse it and get IDS. The trick here is not to get the default URI object by JFactory::getURI() because things will get ugly, use JFactory::getURI('YOU_URI_NAME').
<?php
// You need to get Your own uri, you do not want to modify default URI
// because this will messup a lot of things
$uri = JFactory::getURI('MyCustomURI');
// Test # 1 [ID = SECTION_ID]
$url = "index.php?option=com_content&view=section&id=SECTION_ID";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
// Test # 2 [ID = 123]
$url = "index.php?option=com_content&view=section&id=123";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
?>

codeigniter category -> subcategory -> subsubcategory system

ok, so i'm very new to codeigniter and from what i have learned by now i can't figure out how can i create a dynamic category -> subcategory -> subsubcategory system. Can you give me some guidelines please...some references, anything to guide me on what should i learn to accomplish that? thanks
i should get my URL like this www.site.com/category/subcategory/subsubcategory/etc... , you know what i mean.
I have done this for the page manager in PyroCMS but it is no easy task.
Each page has its own slug and parent_id, then to read the correct page it loops through each of the page slugs and joins the child. It knows how many children there are so if there are 5 children it selects the 5th self-joined table.
Here is an example of the code:
public function get_by_path($segments = array())
{
// If the URI has been passed as a string, explode to create an array of segments
if(is_string($segments))
{
$segments = explode('/', $segments);
}
// Work out how many segments there are
$total_segments = count($segments);
// Which is the target alias (the final page in the tree)
$target_alias = 'p'.$total_segments;
// Start Query, Select (*) from Target Alias, from Pages
$this->db->select($target_alias.'.*');
$this->db->from('pages p1');
// Loop thorugh each Slug
$level = 1;
foreach( $segments as $segment )
{
// Current is the current page, child is the next page to join on.
$current_alias = 'p'.$level;
$child_alias = 'p'.($level - 1);
// We dont want to join the first page again
if($level != 1)
{
$this->db->join('pages '.$current_alias, $current_alias.'.parent_id = '.$child_alias.'.id');
}
// Add slug to where clause to keep us on the right tree
$this->db->where($current_alias . '.slug', $segment);
// Increment
++$level;
}
// Can only be one result
$this->db->limit(1);
return $this->db->get()->row();
}
It's a bit nuts but it works perfectly. This can be really slow so PyroCMS also maintains a look-up table which has id and the page URI to match quickly.
You can see the whole model here:
http://github.com/philsturgeon/pyrocms/blob/master/application/modules/core/pages/models/pages_m.php
you could:
create controller category, reroute some URIs to it and use it's internal logic to parse it's arguments to pick whatever article client requested:
About URLs:
http://codeigniter.com/user_guide/general/urls.html
About URI routing:
http://codeigniter.com/user_guide/general/routing.html
I agree with Phil's idea and I was also envisioning that you can create a separate module (if you use modular extensions for example) to handle the categories in a generic way. Then you can reuse that module in any other projects. Basically the new module may be able to handle categories and sub-categories (the hierarchy).

Resources