CodeIgniter Best Practice Items List - php

I'm creating an ebay-style (but more simplistic) shop in CodeIgniter and I'd like to know the best way to handle the items code/controllers.
At the moment, I've got a Category controller which is the main controller (in routes). /category/id is supposed to show a list of items for a specific category, and /category or / shows all items.
The category index($id=0){} in the controller loads the category helper which generates the category listing, then I echo it out in the view.
Now I'm up to the stage of adding the items aspect... Users need to be able to add items, view specific items, and the category code needs to be able to show all items or a specific category of items.
I was thinking of having a items controller with add_item, view_item, however now I'm wondering how I should be fetching the category listing from within the categories controller.
Should I have a items helper that loads the items model, fetches the list of items based off the category, assigns the list to a variable and echos the list? And when a user wants to view a specific item it loads the item controller view_item, and for adding add_item etc?
I also want the categories to display whilst they're viewing an item, so if I do it this way I'll also need to load the category helper within the items controller... Is this bad practice due to code repetition?

In short, here's what I would do.
/, Shows all items.
/category/xyz, Shows all items of category XYZ.
/item/view/xyz, Shows item XYZ.
/item/add, Adds an item.
...
It's normal to reuse your models across your controllers. It would be scary otherwise; it would mean a lot of code duplication. It's also normal to reuse your helper as much as possible. That's the goal of an helper! To implement a function that is reused at a lot of places. When you start to do copy + paste of the same lines, that's when you are not doing reuse. Create a function and reuse it.
Keep in mind,
Models deal with your data. (Categories, items, bid, ...)
Controller deals with your HTTP requests and responses. (Loading the right models, outputting the right views, ...)
Helper deals with frequent operations. (On your models, on your views, on your requests, ...)
Hope this helps.

Related

Route/API convention questions

I have a web page where I have products and product categories, I have many pages to show data like where I show products belonging to a category, I'm scratching my head about how to approach this.
//Uses ProductController, shows products
Route::get('/products/category/{id}', 'Web\ProductController#productsCategoryId');
//Uses ProductCategoryController, shows products too
Route::get('/product-category/{id}/products','Web\ProductCategoryController#productCategoriesIdProducts');
I have two routes which show the same data, both show products belonging to a category, my heart tells me I should go for the first one in ProductController since I'm ALWAYS showing products, products are the main player per se but I have also seen the second route pattern used, then there's a third option I just thought.
What about forgetting about verbose routes and using query params:
/products?category=1
Boom, afaik if I do this there would be a single entry point in my controller and from that method depending on the query params I would show different pages, index.blade, show.blade, category.blade.
Using query params make me understand/read the url better imo but I'm worried about having a single method with a bunch of if conditionals is the way to go about this...
I'm open to all kind s of suggestions.
Try this one
Route::get('/products/category/{id}?category', 'Web\ProductController#productsCategoryId');
category=1

Create a block in Codeigniter that would generate same output from controller

I have a news blocks. These news are got from the database. What I need is to write $this->load->view('news'); on any of my pages and for the news to display. This implies that 'news' is a view and has display logic for the news AND has logic that calls the controller which calls the model to get the news (this part is what bothers me). I don't want to pass news to the view and inside the view pass it to the 'news' view.
Because news are going to be on roughly 70% of pages, I would like for this system to work (I want to use other views in such fashion). Is this possible?
You can check the Codeigniter HMVC extension:
Modular Extensions - HMVC
This is most useful when you need to load a view and its data within a view. Think about adding a shopping cart to a page. The shopping cart needs its own controller which may call a model to get cart data. Then the controller needs to load the data into a view. So instead of the main controller handling the page and the shopping cart, the shopping cart MVC can be loaded directly in the page. The main controller doesn’t need to know about it, and is totally isolated from it. You can work the same way with your news block.
I think it's a little bit tricky to try to explain everything on a simple SO answer and it has good documentation you can check out there.

list all products and link each category grid with category box in opencart

I want to load all categories grids on one page and want all products to be available on scroll down. currently I need to click on each category and the page refreshes for each category. This is the site I am working on(ahmad.esy.es). I want this type of functionality upon clicking the category(http://www.just-eat.co.uk/restaurants-hertsplaice-en11/menu#2254). kindly suggest me what changes should I make in view files (product.tpl, category.tpl) or controller files(product.php, category.php). how can I load all the grids of categories in one page
I didn't look at the site you have posted above - if you cannot describe your issue by words or by submitting a code or image it should be improved.
From what I understand you just want an infinite scroll to display all the products you have in your eshop. It is not mentioned whether you want to preserve the default functionality (category tree with products attached to certain categories) or whether you want only this one listing - basically it does not matter - so I will describe in short how to add such new functionality.
The model
you do not need to touch any model - you can reuse the method ModelCatalogProduct::getProducts()
this will give you the possibility to sort or filter the products except you will have to omit the filter_category_id to load all the products
The controller
feel free to create a new controller (e.g. ControllerProductInfinite) or just to reuse one of the already existing - either ControllerProductCategory or ControllerProductProduct
all you need is basically the same as in ControllerProductCategory::index() except adding filter_category_id into a array passed when calling ModelCatalogProduct::getProducts() and a new template for rendering
The template
create a new template that will meet your expectations and infinite scroll requirements
stick to any infinite scroll plugin/implementation you like (may Google be with you)

Flexible sidebar database design

In my system I have a flexible layout, where you can place your sidebar left or right (or both). You can add multiple sidebars and then choose which one you want to use for your page, and on which location.
What is the best way to store the items in a sidebar, while maintaining the ability to edit the item configuration per sidebar?
The items consist of a php class in which the config options are passed through to a template file.
My origional thought was to implement some sort of shortcode functionality and add a shortcode to a sidebar_item entry in the database, but this way it wouldn't really be configurable.
If I understand you correctly, you have:
a user, who can place a sidebar where he wants to,
a sidebar, that can contain some elements,
items, that can belong to each sidebar to each user,
files to handle items of different types.
and every user has his own sidebar with his own content.
I can propose tables:
users in which users are defined by some id,
sidebars in which you say which user it belongs to, and if it is left or right,
item_types where you say which template and/or configuration files should be used,
items in what type it is (from item_types), so you know which template to use, then in which sidebar (from sidebars) it is, maybe what position it is (first, second, ...).
You should create relations between:
sidebars and users on user id so selecting user you could know which sidebar(s) he owns,
items and sidebars on sidebar id, so knowing sidebar you know which items are in it,
item_types and items on item type so you know how to handle it,
Perhaps, in items you can also store configuration variables that you could pass to template.

How to handle the subpage concept under Yii?

This is a very newbie question so bear with me. I'm starting to use Yii as my first PHP framework and so far so good, the project on wich I'm learning is a simple informative webpage, but how am I supposed to handle the subpage concept under Yii? This is what I'm trying to achieve:
Home
Products
Product 1
Product 2
Contact
I have a controller for Home, Products and Contact, now I know that Yii doesn't work with subcontrollers, then how do I create a Product 1 and 2 subpages? Just a different view for each one? Through Gii? Many thanks.
You could do one of two things
You could have a generic product page that accepts a parameter to distinguish between different products (common approach). For example,
www.mysite.com/products?id=1 would show Product1's page whereas www.mysite.com/products?id=2 would show Product2's page (and if there is no id parameter in the query string, then you could just show your Product page)
And you could also have separate methods for each page. So you would have
actionProduct
actionProduct1
actionProduct2
methods in your Product controller and then you could reach your pages as
www.mysite.com/product
www.mysite.com/product1
www.mysite.com/product2

Categories