Manage cache Magento 2 using command line

Manage cache Magento 2 using command line – In Magento 2 there are 2 ways to manage cache, going to “System -> Cache Management” in backend or using command line.

You can run commands in following ways:

  • cd <your Magento install dir>/bin and run them as ./magento <command name>
  • php <your Magento install dir>/bin/magento <command name>

1. View the cache status

magento cache:status

You can get a result like this:

1 means “Enabled”  and 0 means “Disabled”

2. Enable or Disable cache types

This command enables or disables all cache types or only the ones you specify

You can get each cache type code and its description here.

 

Above commands will enable/disable all cache types. You will get the cache types status after running the command.

3. Clean and Flush cache types.

What’s different between “Clean” and “Flush”

  • Cleaning a cache type deletes all items from enabled Magento cache types only. In other words, this option does not affect other processes or applications because it cleans only the cache that Magento uses.Disabled cache types are not cleaned.
  • Flushing a cache type purges the cache storage, which might affect other processes applications that are using the same storage.Flush cache types if you’ve already tried cleaning the cache and you’re still having issues that you cannot isolate.

Commands:

Please note:

magento cache:flush is equivalent “Flush Storage Cache” in System -> Cache Management

magento cache:clean is equivalent “Flush Magento Cache” in System-> Cache Management

That’s how we can manage cache Magento 2 using command line. 

Magento 2 get Base URL

Magento 2 get Base URL – As you know, in Magento 1 we can easily get base url using function:

In this tutorial, let’s find out how to get base url in Magento 2

In Magento 2, we can get base url using an instance of StoreManagerInterface 

1. Create an instance of StoreManagerInterface in __construct function.

Then we can get base url using function

We also can get url for media, static, js directory:

2. In another way, we can create an instance of StoreManagerInterface using ObjectManager:

That’s how to get base url in Magento 2. See you in the next tutorial.

Magento 2 Add a menu item in backend

In this blog post we will see how to add a menu item in backend for your Magento 2 module.

1. Create file VendorName/ModuleName/etc/menu.xml

The admin menu in Magento 2 has 3 parts:

  • The main admin menu title which shows up in left sidebar
  • Submenu title
  • Actual menu

2. Create file VendorName/ModuleName/etc/acl.xml

3. Create Admin Routes with file: VendorName/ModuleName/etc/adminhtml/routes.xml

with content:

4. Create a backend controller to handle menu item url

VendorName/ModuleName/Controller/Adminhtml/World/Index.php

Finally, when you click on the new menu item, you will see “Hello World” in a page.

If something is wroing, you will be redirected to dashboard automatically.

That’s how to add a menu item in backend Magento 2, see you in other posts.

Magento 2 – Create system configuration – system.xml

In this blog post we will see how to create System Configuration for your module in Magento 2

System configuration in Magento 2 is very similar to Magento 1

System configuration is divided into different parts; tab, section, group and field which is described in magento1 blog. This same concepts are used in Magento2 as well.

1. Create VendorName/ModuleName/etc/adminhtml/system.xml file

with content

2. Set default values for these configuration item

Create file: VendorName/ModuleName/etc/config.xml

3. Create ACL file

Create file: VendorName/ModuleName/etc/acl.xml

Now when you open Magento admin, you see the “Test” tab and sections.
You might get an error 404 Not Found first time, just logout and login again to fix this.

That’s how to create store configuration in Magento 2. See you in other tutorials.

 

Magento 2 Fix Issue “No Region Found within the Locale…”

In Magento 2 sometimes, an error displays: No region found within the locale “de” (or other locale code). In this topic we will give you a solution for this issue.

How to get bug “No Region Found within the Locale…”

You have to setup language for your browser. Example with google chrome: Setting → Language → Choose your language → Done No Region Found within the Locale Magento 2 No Region Found within the Locale Magento 2

Then please refresh your browser you can get the bug if your extension use \Magento\Framework\CurrencyInterface

How to fix it?

We should use try catch function when define CurrencyInterface

Refresh your browser and this issue has gone.

That is the shortest process for you to  Fix Issue “No Region Found within the Locale…” in Magento 2 . With this guide, you can manage the Issue “No Region Found within the Locale…” in Magento 2 easily.

Thank you for reading this post and see you in other posts

Magento 2 Compile error: “No tokens were provided”

Magento 2 Compile error: “No tokens were provided”

In Magento 2, when you run “setup:di:compile” you may get error message:

“No tokens were provided”

That error message is caused by an empty file. You can find it using command:

It usually comes from an additional extension, just delete the empty file and run compilation again using:

I hope this will help. If you have any question, leave a comment or contact us.

Thanks.

Magento 2 Render WYSIWYG content

Render WYSIWYG Magento 2 – In this tutorial, we’re going to tell you how to render WYSIWYG Magento 2.

Some cases, you save your data using WYSIWYG editor then you want to render it in frontend.

With Magento 1, you can do it in this way:

However, in Magento 2, there is no BlockTemplateProcessor. We need to use this method instead:

Hope this could help.

Magento 2 Send Email Programmatically

Magento 2 send email programmatically – In this post, let’s find out how to send email programmatically in Magento 2. It will be useful for you in your custom module or new feature. What we need to do:

  • Create your email template in a html file.
  • Declare your email template.
  • Create a function to send your email.

We’re doing step by step:

Create new html file

/app/code/VendorName/ModuleName/view/frontend/email/modulename/test.html

with simple content:

Declare your email template, create xml file

/app/code/VendorName/ModuleName/etc/email_templates.xml

with content:

Finally, create a function to send your email.

In this tutorial, we’re using a controller action. Create file:

/app/code/VendorName/ModuleName/Controller/Index/Sendemail.php

with content:

You also can create this function in helper, model …

If you have any question, just leave a comment or contact us.

Thanks.

Magento 2 How to add link on my account navigation

How to add link on my account navigation in Magento 2 – In this tutorial, we’re going to add a new link to my account navigation in Magento 2.
You only need to create a layout file:

/app/code/YourVendor/YourModule/view/frontend/layout/customer_account.xml

with content:

In this file “mymodule/index/index” is url path of your link and “My Custom Link” is the label.

After flushing cache, your account navigation should look like this:

How to add link on my account navigation in Magento 2
How to add link on my account navigation in Magento 2

Magento 2 Registry object

Registry object in Magento 2 is one of the most useful objects.

In Magento 1, we can simply use register and registry using static method of Mage class:

In Magento 2, it supports this method also. However, we need to use class  \Magento\Framework\Registry. You can init an instance of this class in class constructor.

 

To save a variable, we use this code:

To retrieve this variable:

Registry object in Magento 2 need more steps to be usable but it’s still very helpful. So please note this, you will need it in your Magento 2 module.