I have an application which I am trying to build using Slim PHP and at the moment I am struggling with one Route I am trying to sort out. Basically what happens is I go to GET /:slug and this provides me with a product view. However if there are variations on this product, it will be an intro page in which a visitor can target an exact product. The differences in these products will be :
Color
Dial
Strap
Material
Possibly a few others. The problem I am having is that I am not 100% sure which if any there will be. Do I write my routes for every possibility? Or is there a way I can get this to take a more relaxed approach to how this is formed?
I saw an example where someone did the following :
$app->get("/:slug(/:dial(/:strap(/:material)))", "Route\To\Controller:Action");
However I am assuming this will require this to be in that set order?
Also a note to add would be that some products have no additional filters and respond directly to /:slug whereas others may require all of the additional filters.
These results are all set in the database. I grab all products that have a similar slug as it equates to the product name, then each filter or collection of filters will be determined by certain parts in the database. For example: /this-product/this-dial/this-color/this-strap/this-material all of these values will be stored within the database table for the product - they either have a corresponding value for material - or it is NULL.
This allows me to on the root URL with the SLUG I can show variations - which will aid in the shopping experience in my opinion.
Does anybody have a solution?? Anyway I could achieve this? Am I going about it the wrong way?
One possible way is to render different templates based on the number of returned products. For example, if you are using Twig Template extension you can easily iterate over the set of results and render two templates one for Product Description and one for Product Group.. Alternatively, You can handle this while rendering a view in your SLIM Route
$app->get('/:slug', function ($slug) use ($app) {
$db = new dbConn();
$Products = $db->ProductQuery(); // DB querying for 'slug' & returned product (s)
if( // here check how many $Products and if = 1 ) {
$app->render('single_product_view.html',$Products);
} else if( /// > 1 ) {
$app->render('multiple_product_view.html',$Products);
} else {
$app->response->redirect($app->urlFor('notFoudnt'), 404); // or whatever other route..
}
})->name("product");
As for the querying why do you want to have a route for each product attribute ?
Can't you in this case have one route for the product view and add a query parameter ( attributes ) to URL
/this-product?dial=this-dial&color=this-color&strap=this-strap&material=this-material
$app->get('/:slug', function ($slug) use ($app) {
$Dial = $app->request()->get('dial');
$Color = $app->request()->get('color');
$Strap = $app->request()->get('strap');
$Mat = $app->request()->get('material');
// same as above, build your query string and render corresponding view
})->name("product");
You can also Group your routes ( for ajax request maybe.. )
$app->group('/api', function () use ($app) {
// Products attributes group route
$app->group('/products', function () use ($app) {
// Get Products with color = :color /api/products/color/:color
$app->get('/color/:color', function ($color ) {
});
// Get Products with Strap = :strap /api/products/strap/:strap
$app->get('/strap/:strap', function ($strap) {
});
// and so on..
});
});
Related
I'm writing an Omeka Plugin and wants to get the list of all public Items with their all elements, in a controller under my plugin.
I've tried get_items() but the function doesn't exists, it looks like the function is only available for the views - not sure how.
another try was to manually fetch the records from database, but that's not a standard way.
So, the question is, is there a predefined function/class or way to get all the items in a controller?
I'm not sure if there is a function that will get you the items along with all of their element texts, but if you want a list of items, inside the controller you should be able to make a call like:
$items = $this->_helper->db->getTable('Item')->findAll();
The Omeka docs warn against getting all the items at once because it could be memory intensive. So, alternatively, you can loop through items.
$items = $this->_helper->db->getTable('Item');
$item = $items->findFirst();
while($item != NULL){
// Do something
$item = $items->findNext($item);
}
There is a "public" property on an item that tells you if it's public. In order to get the element texts for an item, I think you'd have to make a query on the ElementText table.
For more information, see the Omeka read the docs page for Table_Item, Omeka_Db_Table and Item:
http://omeka.readthedocs.io/en/latest/Reference/libraries/Omeka/Db/Table.html
http://omeka.readthedocs.io/en/latest/Reference/models/Table/Item.html
http://omeka.readthedocs.io/en/latest/Reference/models/Item.html
I'm trying to return search results to a new controller than where the search action was performed from. Problem is Results is never accessible from CustomSearchResultPage.ss. I've added inline comments for what I think is happening, am I right in my thinking here?
// Customise content with results
$response = $this->customise(array(
'Results' => $results ? $results->getResults() : '',
));
if ($results) {
$response = $response->customise($results);
}
// Use CustomSearchResultPage.ss template
$templates = array('CustomSearchResultPage', 'Page');
// Create a new CustomSearchResultPage page
$page = CustomSearchResultPage::get_one('CustomSearchResultPage');
// Build a controller using the CustomSearchResultPage
$controller = CustomSearchResultPage_Controller::create($page);
// Add the custom data to the newly minted controller
$result = $controller->customise($response);
// Return the controller and tell it how to render
return $result->renderWith($templates);
The page seems to render as expected just the variable is always empty...
Your explanation is a little hard to follow I'm afraid. So I'm answering for what I can ascertain as below:
Performing a search. This requires loading a controller to do as such.
Customising the current controller with the results
RE-customising the current controller with itself.
Setting the template for the current (double customised) controller.
Disregarding all of the above.
Fetching a random page (or an empty record).
Creating a controller for the empty page.
Customising the new controller with the customised controller of the current controller customised with itself.
Returning that page, which shows no results.
You need only stop at step 4 (skip step 3), and return the customisation ($response).
If there is some reason you think you need another controller however (which seems superflous, but who knows), creating and then customising that one (only) before returning it would be better.
Being that you have only used this second controller for rendering a result, the URL will not have changed or anything. The whole thing seems beyond requirements.
A much more simple way to render a result from this action would probably be:
return $this
->customise(['Results' => $results ? $results->getResults() : null])
->renderWith(['CustomSearchResultPage', 'Page']);
(from the top of my head, may need a little refining).
I'm building my first prestashop. I have a couple of products with attribute combinations that have an impact on the price. In list views, I would like to detect if a product has more than one combination attached to it, in order to display a 'from' before the price.
I have not been able to find a way to access anything related to attributes or combinations from the product_list.tpl.
I found a function in product.php that might be suitable for what I'm trying to achieve.
public function hasAttributes()
{
if (!Combination::isFeatureActive())
return 0;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(*)
FROM `'._DB_PREFIX_.'product_attribute` pa
'.Shop::addSqlAssociation('product_attribute', 'pa').'
WHERE pa.`id_product` = '.(int)$this->id
);
}
From product_list.tpl I can access other stuff inside the product class like e.g. 'features' and I was hoping to get the attributes in a similar way.
The only place I could find 'features' being declared as a variable was in the product controller, as part of this array:
$this->context->smarty->assign(array( ...
So I assumed that's the way to go, add a variable and point to the desired function inside the product class. but no matter what I enter here it just doesn't work out. What am I doing wrong? Is this the right approach at all?
Thanks in advance.
You can view the site at rhum-martinique.de
You can use the method ProductCore::getAttributeCombinaisons and check its result with the count function
Add this function to the file /config/config.inc.php
function count_product_combinations($id_product)
{
$product = new Product($id_product);
return count($product->getAttributeCombinaisons(Context::getContext()->language->id));
}
Add this code to your tpl file:
{if count_product_combinations($product.id_product) > 1}
<span class="price-prefix">{l s='From'}</span>
{/if}
I'm completely new to ci,
I have a url something like this:
http://localhost/mvc/post/prod_id/1
And I want it to be:
http://localhost/mvc/post/my-best-product
So far I'm able to manage to route all that to home/post/ and learned the segment function also.
But my question is how do I really get the url_title out to the actual url.
I couldn't find any information on this particular subject. All I could find is how to use the url_title and how to route in ci. But they don't explain how we can actually change the base url name.
Please guide me to the right direction.
solution Example:
public function my_method($product_slug)
{
$product1 = "training-for-recruitment";
$product2 = "training-for-od";
if($product_slug==$product1)
{
$this->load->view('prod1');
}else if($product_slug==$product2)
{
$this->load->view('prod2');
}else{
show_404();
}
}
This is not what exactly I'm going to do. It is just for others to understand the workaround of Slugs.
Generate a unique slug for each of your product. Add a field for it on product table and every time, while selecting a product from table, use that slug instead of getting the product from primary id.
So, your function becomes like:
function product($product_slug)
{
//get product by slug from database
//load view page
}
Now, in config/routes.php
$route['your_controller_name/(:any)'] = "your_controller_name/product/$1";
You need to do this:
Set up some logic that translates "my-best-product" to "1"
Set up routing in CI that calls your 'prod_id' controller and passes the URI vars
I want to be able to choose a controller based on data gathered form the uri.
I have a categories table and a subcategories table. Basically I have a URL in the following format (:any)/(:any). The first wildcard is a city slug (i.e edinburgh) and the second is going to be either a category or a subcategory slug.
So in my route I search for categories with that route, if I find it, I want to use controller: forsale and method: get_category. If it's not a category I'll look up subcategories, if I find it in there I want to use controller: forsale and method: get_subcategory. If it's not a subcategory I want to continue looking for other routes.
Route::get('(:any)/(:any)', array('as'=>'city_category', function($city_slug, $category_slug){
// is it a category?
$category = Category::where_slug($category_slug)->first();
if($category) {
// redirect to controller/method
}
// is it a subcategory?
$subcategory = Subcategory::where_slug($category_slug)->first();
if($subcategory) {
// redirect to controller/method
}
// continue looking for other routes
}));
First off I'm not sure how to call a controller/method here without actually redirecting (thus changing the url again).
And secondly, is this even the best way to do this? I started using /city_slug/category_slug/subcategory_slug. But I want to only show city_slug/category|subcategory_slug but I need a way to tell which the second slug is.
Lastly, there may be other URL's in use that follow (:any)/(:any) so I need it to be able to continue looking for other routes as well.
Answer to your questions in order:
1. Instead of using different controller#action's you could use a single action and based on the second slug (category or subcategory), render a different view (although I don't like this approach, see #2 and #3):
public class Forsale_Controller extends Base_Controller {
public function get_products($city, $category_slug) {
$category = Category::where_slug($category_slug)->first();
if($category) {
// Do whatever you want to do!
return View::make('forsale.category')->with(/* pass in your data */);
}
$subcategory = Subcategory::where_slug($category_slug)->first();
if($subcategory) {
// Do whatever you want to do!
return View::make('forsale.sub_category')->with(/* pass in your data */);
}
}
}
2. I think /city_slug/category_slug/subcategory_slug is way better than your method! You should go with this one!!
3. Again, you should revise your routes. I always try to make my routes in a way that they don't confuse me, neither Laravel!! Something like /products/city/category/subcategory is much more clear!
Hope it helps (my code is more like a psudocode, it's not been tested )!