Is there any way to change url in Laravel 5.5? - php

I am developing a site with Laravel 5.5
I am going to hide the product slug when customers visit single product page.
original url: http://localhost/laravel/product/category-name-product-slug (this is product slug in database)
must be changed to: http://localhost/laravel/product/category-name.php (or html)
The problem is that I have to send the product-slug to controller so that know about what product is needed instead of product ID.
Also I have to show category name(this must be substring of product-slug: substr($product_name, 0, -10)) on browser url.
How can I solve this problem?

If you can't put it in the path you'll have to use a query string.
Retrieving Input From The Query String
e.g. http://localhost/laravel/product/category-name.php?product-slug=product-slug
and if you have to do that you might not lose any seo by also passing the category and product ids in the query string.

I have solved this problem by using form.
I have created a form in blade and made its method to POST.
Defined a hidden variable to represents product slug, and then render a virtual slug name (substring of product slug) to route.

Related

How to update the filters from the Venia store-front like the Magento front view?

Good afternoon everybody,
I am kind of a rookie using magento and I need help with this:
This are the filters from Magento front View, this filters can change position changing the position attribute in the magento/admin page.
enter image description here
this are the filters in the Venia-storefront:
enter image description here
As you can see there are not in the same position. So my question is, is there any way to connect what its happening to the position in the Magento front to affect the Venia-Storefront equally?
Can anyone help me with this?
Updating a little bit:
What I tried was to extend my graphql schema to have that position value & I did it, here is a photo of the schema:
When I use the query in the graphql playground I got the attribute correctly:
I got even the correct attribute in the json in venia-frontpage:
But as you can see the value is null.
If you go to the web admin page in magento, you can see that they have values in the position attribute:
Is there any way to grab that data in the admin and take it to the position value in the schema?
I have to change the resolver? what can I do to accomplish this?
here are the photos of the resolver:

How to Pass slugged url to Controller in Laravel

Hey Guys I am new to Laravel and I am currently trying to pass a slugged url into my controller, but I am having issues with it.
I have a table for categories and I have all the categories looped through and displayed on a view page (/posts). I listed all the categories on this view, so that when a user clicks on a category, they will be taken to a different page showing all posts belonging to that category(e.g, /posts/category/{categoryID}). I know I can get this done by passing the category id to the controller and finding the id to pass back to that view. However, I am trying to do something different.
I am looking to pass the category name instead of the id to the controller. E.g, /posts/category/{category name}. But due to the fact that some categories are more than a word (e.g, Dry Cleaning), it has become a challenge for me. I had earlier slugged the URL for SEO purpose (e.g, /posts/category/{Str::str(categoryname)}) and from the web.php file, I created a get route.
Now, I'd like to fetch all posts relating to the category name passed in. The issue is that the category names are slugged.
Is there a way to remove the slug("-" or hyphen) from the name passed into the controller? So, instead of "Dry-cleaning", it will come back as "Dry cleaning" which matches the name in the database.
I am trying to avoid passing id to the url.
Thanks.
Good question.
There are a couple of approaches you can use to tackle this problem. I'll run through them for you.
1. Store the slug in the database
One option is to store the slug name within the database. Personally this is my favourite option as you don't need to do any string transformations. With this approach I would put a slug column in your categories table. Then you can use the Category eloquent model to search for a category with that slug.
2. Convert the slug to title case
The second option is to convert the slug to title case. You can do this by using Laravel's string class. For example:
$categoryName = Str::title($slug);
However there are a few issues with this approach. Firstly, when creating a slug the url will remove characters that are not safe, for example an apostrophe. This means if you have a pluralised category name it won't match when converting the slug back to title case.
Secondly, by using code to transform strings in order to match a record in the database you are at the mercy of the service class. While the functionality of Str::title is unlikely to change, it could in theory which could then mean you have to change code later on down the line.
Conclusion
Overall, I would recommend going with the first approach. You have much better data integrity and less chance of things going wrong further down the line. So if I were you I would create a new migration to add a slug column to your categories table and then populate each category with its own slug. Then search for a category based on the slug column when querying your eloquent model.

How to filter post types with a select field?

I'm working on a Wordpress project and have a question.
I have a layout like shown below in the link to screenshot with a select field and boxes with news (two or more different post types). I want to filter with the select the post type so there should show up only the selected. How can i do that?
Thanks in advance.
If you're fine dropping the pagination and having all of your posts on a single page, one option is to use Isotope.js. In addition to being a very useful plugin, they have thorough documentation to figure out how to structure your code to get filtering working.
If the pagination is absolutely necessary, this becomes more complex. Here's the short hand-wavey version that needs testing: You can use JS to reload the page with an extra parameter added to the URL (ex: /your-page?filter=post_type_1). You'll need to register your custom URL parameter with WordPress. Then, in your PHP, you'll use that parameter data in your WP_Query (or whatever you're using to query posts) to grab only posts of that type. It would be smart to have a default, where if the filter parameter from the URL isn't present or doesn't match a post type you have in your site, you default to showing all posts.
This blog post walks through how to do this method of filtering more thoroughly: https://codepen.io/the_ruther4d/post/custom-query-string-vars-in-wordpress.

Add custom field to Magento Admin (URL field)

I'm trying to add a custom field in the Magento backend, at the Configuration -> Web section. I want to add an additional URL text space (can be in any of the sections, URL options, for example, would work well). How can I do that? I have seen other people who tried it by making a custom module, but that was for the category section. I need it at the Web section though, have not had success yet.
Thank you!
You can add an custom field to the web section. The best approach is to create a custom module for this.
In your system.xml you need to add your section to the Web section.
More information on this blog.
If you want to add attribute for product (can be text string named URL text) you can do it in catalog/attributes/manage attributes here you can create a new EAV attribute.
Next go to catalog/attributes/ manage groups/ and there you can drag/drop attribute to the correct group (will be shown in product admin page in the left list of groups)
In template you can call a attribute by call $_product->getAtributteName (if the attribute was named atributte_name) or $_product->getAnotherAttributeName (for attribute named another_attribute_name).
Always make sure if attribute is enabled for catalog / or product view you are using to make possible "get..." call from that view.
If you want it for using at URL, you can edit URL call by adding it to the end of URL as a GET parameter example:
http://www.somestore.com/?yourproductorcategoryurl&myparmforurl=<?php echo $_product->getSomeAtributteName ?>
if you using attribute type list (list of defined values, it always return an id of attribute value, not name) then you can use $_product->getAttributeText('name_of_your_attribute')
I was able to do this via the official Magento blog: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_add_a_custom_module_for_custom_admin_config_options
Thank you for your help though!

Calling a product attribute programmatically in the product description in Magento

Didn't see this question asked anywhere, so here goes:
I have several attributes in my Magento store, but to spare the user from an overly cluttered page, I've separated different kind of information on the product pages with tabs. The first tab displays the product description which currently is a bunch of HTML which basically mirrors the attributes by displaying easy-to-understand icons with the attribute value beneath them and the second tab displays more in-depth information with the attributes called from the DB.
Right now, when editing or adding a product, I will basically have to add the attribute twice: once as an actual product attribute and once for the simple HTML product description with icons.
What I would like to do instead, is to call the attribute programmatically and insert a variable that would load the attribute value under the icon automatically, thus sparing me hours in manually editing and double-checking every value.
A basic line of sample code without some other things currently looks like this:
<img src="http://myurl.com/img/profile.jpg" alt="attribute_1_image" /><br /> 70mm
Instead of writing out the 70mm manually, I'd like to be able to call it from the database, so I could simply copy and paste the same HTML on every product description page, since I already have 70mm added as attribute_1, so the preferred outcome would be:
<img src="http://myurl.com/img/profile.jpg" alt="attribute_1_image" /><br />{var.attribute_1}
This, or anything similar where it would simply load the attribute from the same product page the user is currently on.
The closest thing I've come to, is this: http://www.devinrolsen.com/magento-custom-attributes-value/ but I realize this can't be done via PHP since the editor will not support it, so I'm stumped as to how I could achieve this. I'm running Magento 1.6.
Any help is appreciated!
Thanks.
You can use your custom "variable" as a text replace. Put {var.attribute_1} in the description from the backend. Then, in product view, do a string replace.
File app/design/frontend/default/YOUR_THEME/template/catalog/product/view.html:
$description = $_product->getDescription();
if ($_product->getVariable1()):
$description = str_replace(
'{var.attribute_1}',
$_product->getResource()->getAttribute('variable_1')->getFrontend()->getValue($_product),
$description);
endif;
and so on for each attribute, then echo $description;. You have to enable the attribute for product view in Magento backend. No extension needed.
This makes sense only if you have different description "patterns" for your products. Otherwise (for example if variable1 is always present) you can just put your <img> tag in this file followed by the attribute value, and keep the description field for an actual description.
Edit: to prevent {var.attribute_1} to be shown in the product view even while the actual attribute is empty, the following code looks better:
$description = $_product->getDescription();
$attribute_value =
($_product->getVariable1()) ?
$_product->getResource()->getAttribute('variable_1')->getFrontend()->getValue($_product)
: "";
$description = str_replace(
'{var.attribute_1}',
$attribute_value,
$description);
Sorry to aswer that, but i'm unsure product attribute can be directly inserted in the description field of a product (if that's you wanna do?)
The good way is to have a block on description page and do the layout in there. With html and php like the link you pointed.
After you just have to display this block on the product page and it will load and display attributes for all our products.
Hope this helps.

Categories