How to create dynamic route in Laravel? - laravel

I create compare page for products,
I need create route for compare page, How to create dynamic route in Laravel 5.3?
For example:
https://www.mywesite.com/Compare/DKP-181451/DKP-254287/DKP-254282/DKP-227429/DKP-254282
This page is 5 product for compare.
Or:
https://www.mywesite.com/Compare/DKP-181451/DKP-254287
This page is 2 product for compare.

The following will probably work:
Route::get("Compare/{any}", function ($any) {
$productsToCompare = explode("/",$any);
// Compare products.
})->where("any", ".*");

Related

How to convert CodeIgniter urls into user friendly ones?

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.

orderBy-querybuilder and pagination in laravel 5

I'm trying to make an orderBy in my query and a pagination for the items from a gallery, but instead of sort the complete query, laravel order only page-by-page content, so if I order desc it only works 1 page, when I go to the next page I can find values really different (ordered, but without relationship with the first page).
Here is my query:
$product = DB::table("product")
->leftJoin("provider","product.provider_id","=","provider.id")
->orderBy("daily_sales","DESC")
->select('product.*','provider.comercial_name')
->paginate(6);
You can try:
$product = DB::table("product")
->leftJoin("provider","product.provider_id","=","provider.id")
->select('product.*','provider.comercial_name')
->orderBy("daily_sales","DESC")
->paginate(6);

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.

Show page:2 directly in View page in CAKEPHP

Is there any option in $this->Paginator->Settings() to paginate a table and store second or third page to the variable using
$view=$this->Paginate('Post');
You can specify the page with the following:
$this->Paginate->settings(array('page' => 2));
$posts = $this->Paginate->paginate('Post');
$posts now contains the records for page 2

Magento - How to add multiple items to the cart programmatically?

I'm trying to add multiple simple products to the cart at the same time using a query string as below, however it only adds the last product to the cart instead of both:
Can someone let me know what I'm doing wrong?
http://www.domain.co.uk/checkout/cart/add?product=9916&qty=4&product=15749&qty=4
I have also tried this:
http://www.domain.co.uk/checkout/cart/add?product[]=9916&qty[]=4&product[]=15749&qty[]=4
Any help much appreciated!
Add Product To Cart With Querystring
Add simple product in shopping cart with no attribute.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY
Here PRODUCT_ID = 'Product Id',PRODUCT_QUANTITY = 'product quantity to purchase'.
Add product into shopping cart with single custome option.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&super_attribute[OPTION_ID]=OPTION_VALUE
Here OPTION_ID = 'Custom attribute option id',OPTION_VALUE = 'Custom attribute option value'.
Add product into shopping cart with multipal custome option.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&super_attribute[OPTION_ID_1]=OPTION_VALUE_1&super_attribute[OPTION_ID_2]=OPTION_VALUE_2
Here OPTION_ID_1 & OPTION_ID_1 = 'Custom attribute option ids',OPTION_VALUE_1 & OPTION_VALUE_2 = 'Custom attribute option values'.Here add more options in `super_attribute` array
Add Extra products with mail product with only 1 quantity.
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_2
Here PRODUCT_ID_1 and PRODUCT_ID_2 is other products id. add more product by id using `,` separator. Example:- &related_product=1,2,3,4.
Default magento there is not setting for add related product quantity into cart.so if you want to add this code than open app/code/core/Mage/Checkout/controllers/CartController.php find public function addAction().
if (!empty($related)) {
$cart->addProductsByIds(explode(',', $related));
}
Replace with
$rel_qty = $this->getRequest()->getParam('related_qty');
if (!empty($related)) {
$relatedproducts = explode(',', $related);
$relatedqtys = explode(',',$rel_qty);
$i = 0;
foreach($relatedproducts as $relatedproduct)
{
$cart->addProduct($relatedproduct, array('qty'=>$relatedqtys[$i]));
$i++;
}
}
Now use query string for add related products with quantity.
http://yourserver.com/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_2&related_qty=PRODUCT_ID_1_QUANTITY,PRODUCT_ID_2_QUANTITY
If you don't want to change any code, you can try to utilize related products functionality by adding related_product parameter to your request. So your url will look like this:
http://www.domain.co.uk/checkout/cart/add?product=9916&qty=4&related_product=15749
If you want to add more products, just list them with comma separator: related_product=1,2,3
The only drawback from that is that you actually can't specify the qty for related products.
To see how it works - Mage_Checkout_Model_Cart::addProductsByIds(array_of_ids)
If qty for subsequent products is a mandatory for you, you'll need to create your own controller, or override the Mage_Checkout_CartController::addAction method.
I found a cheeky way I found of getting around the quantity limitation of the related_products query string field noted above in other answers. If you just put the SAME ID MULTIPLE TIMES in the value of related_products, as many times as the quantity you need, then that will achieve the same effect as if there was an explicit qty field for each related product. So taking himansu's answer above and adapting it we get:
http://yourserver.com/checkout/cart/add?product=PRODUCT_ID&qty=PRODUCT_QUANTITY&related_product=PRODUCT_ID_1,PRODUCT_ID_1,PRODUCT_ID_1,PRODUCT_ID_2,PRODUCT_ID_2
This will add to the cart PRODUCT_QUANTITY of PRODUCT_ID, 3 of PRODUCT_ID_1, and 2 of PRODUCT_ID_2.
So as long as you're happy doing a little work to generate the same ID multiple times this works a treat. And no custom code changes required on your magento server.

Resources