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

Upload image in Magento 2 programmatically

Upload image in Magento 2 programmatically – In this article, we’re going to tell you how to upload image in Magento 2 using a form. We have a post about this feature in Magento 1, you can take a look here.

What we need to do:

  • a form with a file input.
  • a controller to get request and save the images.
  • Assuming that we save all images in “/media” directory.

1. Create a form with content:

That’s just a simple form with a file input. You can add some elements as labels if you want.

2. Create controller and upload action

Create controller file:

/app/code/VendorName/ModuleName/Controller/Index/Upload.php with content

In this file, please note:

  • ‘test_image’ is the name of form input.
  • protected property $_objectManager is already declared in class \Magento\Framework\App\Action\Action
  • we only need to declare one more property $_fileSystem which is an instance of \Magento\Framework\Filesystem
  • $result  will be an array.

So we can use that form to upload image in Magento 2. After uploading the image, you will get it in ‘pub/media’ directory. Make sure that folder is writable.

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.

 

Magento 2 – admin page 404 after installation.

Magento 2 – admin page 404 after installation. In this post, let’s find out how to fix the error admin page 404 after installation in Magento 2. My environment:

  • Ubuntu server
  • Apache2
  • PHP7

After installing magento 2 , you may get 404 error when visit admin page. In that case, you should check if mod_rewrite module is enabled and loaded using this command:

If you already enabled mod_rewrite, you will get this message:

Or you will get this message if you didn’t enable mod_rewrite:

Then you should run:

In case you still get 404 error after that, open your site config file:

Add or edit:

Then restart your apache:

You can get access to Magento 2 backend now.

Please contact us or leave comment if you get any problem.

Override a Block in Magento 2

Override a Block in Magento 2 – In the last tutorial, you can learn how to create a table and model in Magento 2. Today I’ll tell you how to override a Block in Magento 2.
What we have to do:
– Build a Magento 2 extension structure. I’ll use the module in last tutorial, you can download it here.
– Set preference in di.xml file
– Define a class to override Magento core class.

Continue reading “Override a Block in Magento 2”