1

I have two controller action [1] /app/code/Mycustom/Custompage/Controller/Mypage/View.php and [2] /app/code/Mycustom/Custompage/Controller/Mypage/View/Mytest.php .

I have cretead frontend layout file named selectpage_mypage_view.xml and it's working proper for both controller action but i want to call different layout files for both controller. For second controller action i have created layout file named selectpage_mypage_view_mytest.xml but no luck. Any help should be appriciated. Thanks.

  • You have the name of the layout correctly, look for the error elsewhere, for example in the namespace of the second controller. – Evgeniy Kapelko Oct 13 '18 at 22:16
  • @Raj find the answer below magento.stackexchange.com/a/255327/52244 hope this will help you, if it match your expectation please mark it valid answer so it will useful others – Kanhaiya lal Dec 20 '18 at 11:05
  • @Raj, hope you got solution, if below given answer match your expectation please mark as valid answer so if someone looking for same they can use as their reference. any further help will be appreciated. – Kanhaiya lal Apr 25 at 7:00
1

You can't add subfolder Controller, you should have this path :

app/code/Vendor/Module/Controller/Controlerfolder/Controllername.php => YES

app/code/Vendor/Module/Controller/Controlerfolder/ControlerSubfolder/Controllername.php => NO

SO in your case :

app/code/Mycustom/Custompage/Controller/Mypage/View.php // OK

app/code/Mycustom/Custompage/Controller/Mypage/Mytest.php // OK

app/code/Mycustom/Custompage/Controller/Mypage/View/Mytest.php // KO
  • Hi @PRINCE. So declaring controller in sub-folder is wrong way in M2? – Raj Oct 15 '18 at 4:43
  • 1
    I think so, I struggled once during hours to understand why it does not work, and I don't have an explanation for that, but it doesn't work with a subfolder. – PЯINCƏ Oct 15 '18 at 7:44
  • @PRINCE please check magento compare page logic that support subfolder logic. and see the below given answer which might clear your doubt. magento.stackexchange.com/a/255327/52244 any help will be appreciated. – Kanhaiya lal Dec 20 '18 at 9:35
0

As per asked quetion [1] & [2], keeping same points in mind and giving real example.

In magento 2 we can create sub-folder under controller folder and in that we can create custom controller as well as per our need.

[1] related to product detail page

/vendor/magento/module-catalog/Controller/Product/View.php

so it's access in frontend using url like /catalog/product/view (it detail page so if access need to pass id value as param)

example : catalog/product/view/id/2

and default Layout Handles for this page is catalog_product_view

[2] relate to magento's compare page

/vendor/magento/module-catalog/Controller/Product/Compare/Index.php

so it's access in frontend using url like /catalog/product_compare/index (sub-folder will merge with _ )

So My Opinion : sub-folder controller access path will be like

FRONTNAME / folder (if sub-folders then merge with _) / Controller Name

and default Layout Handles for this page is catalog_product_compare_index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.