Route/API convention questions - php

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

Related

Subcategories and resource controllers?

Im using a resource controller to control articles.
/news
Hits the index method and brings up all the articles under the category.
/news/article-slug
Hits the show method and brings up an article under the category.
My question involves sub categories.
/news/sub-category-name
This would hit the show method which would look for an article with the sub-category-name when I want it to hit the index method and bring up a list of articles for the sub category.
What would be the approach to use for sub categories?
Add another route and method like:
get('/news/{subcategory}', [
'as'=>'subcategory',
'uses'=>'ArticleController#getSubCategory',
]);
Or would it be better practice to scrap sub categories and have a url like:
/news-sub-category-name/article-slug
Or is there another way?
Thinking semantics you would be better to servers category names then the title. ie
www.domain.com/news/category/article
This will also give you a clean approach for your routes file. IE no wildcard matching or regex to replace (cat-) etc.
Thats the way I would personally go.
It use to be in the past you would want to keep your URL's as short as possible but thats just not the case these days. Dont get me wrong thats not to say you want domain/cat/cat/cat.....
... Update
To use resources you would have something like.
Route::resource('news/{cat}/article', 'ArticleController');

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)

Wordpress - Make category.php list posts from subcategories as well

I'm building a blog where three people will be writing, but they will have a separate section on the site. The structure is built upon categories and looks like this:
User one
Clothes
Fashion
Life
User two
Family
Life
User three
Family
Friends
Life
I've set all three users as writers and used the plugin Restrict Categories in order to lock their categories to the sub-categories the matching each top level category.
Now here comes the question, if I visit http://site.example/category/user-one/ I want to have a listing of all the posts, including the sub-categories since there will be no actual content in the top level category that you are browsing.
I've tried making this using filters bound to pre_get_posts but I still can't get it to work. Does anyone else here have any ideas?
I've also given some though into actually creating a multisite network for the purpose, but I then realized it will be hard to fetch posts from all the networks at once.
Thanks in advance, Jonathan
#Jonathan: Not sure if I am misinterpreting what you are hoping to achieve, but given that each author will already have their own 'author' page which lists all their posts (if your theme includes this or something you could create or customise), there may not really be a need for Users 1-3 to be their own category?
Going back to your question, for listing subcategories, perhaps https://wordpress.stackexchange.com/questions/13485/list-all-subcategories-from-category might be of some help.
As for listing posts, to keep things simple, I reckon you might be able to use e.g. get_posts (http://codex.wordpress.org/Template_Tags/get_posts) and build custom arguments, perhaps in a loop if you prefer to use one instead of slug/id specific category templates.
http://codex.wordpress.org/Category_Templates
If it's any help, Bainternet actually created a plugin that makes life a little easier to define category templates: http://wordpress.org/plugins/custom-category-template/

CodeIgniter Best Practice Items List

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.

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