6

I'm doing a simple CRUD custom module after this module

C:\xampp\htdocs\magento\vendor\magento\module-cms\Controller\Adminhtml\Page\Save.php


and I have a controller to save/create/add the "students" to database.
but something is missing here
so the controller only go to the __construct() function
but the execute() function doesn't work
You see, when I var_dump('111'); in the __construct() function, it works fine
but when I var_dump('111'); in the execute() function, nothing happened.

<br>
Here is my controller:
<br>

    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 18/07/2018
     * Time: 1:30 CH
     */

    namespace Fudu\HelloWorld\Controller\Adminhtml\Student;

    use Magento\Framework\Registry;
    use Magento\Framework\View\Result\PageFactory;
    use Magento\Backend\App\Action\Context;
    use Fudu\HelloWorld\Model\StudentsFactory;
    use Fudu\HelloWorld\Model\ResourceModel\Students as ResourceModel;

    class Create extends \Magento\Backend\App\Action
    {
        /**
         * @var StudentsFactory
         */
        protected $studentsFactory;

    /**
     * @var ResourceModel
     */
    protected $resourceModel;

    public function __construct(
        Context $context,
        Registry $coreRegistry,
        PageFactory $resultPageFactory,
        StudentsFactory $studentsFactory,
        ResourceModel $resourceModel
    ) {

        $this->resourceModel = $resourceModel;
        $this->studentsFactory = $studentsFactory;

        parent::__construct($context);
    }
    /**
     * Save action
     *
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $data = $this->getRequest()->getPostValue();

        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
    //
            if ($data) {
                try{
                    /** @var \Fudu\HelloWorld\Model\Students $model */

                $model = $this->studentsFactory->create();

                $model->setData($data);

                $this->resourceModel->save($model);

                $this->messageManager->addSuccessMessage(__('Create Student Successfully.'));

                // Redirect to your form page (or anywhere you want...)
                $resultRedirect->setPath('/companymodule/index/booking');

                return $resultRedirect;
            }
            catch (\Exception $e) {
                $this->messageManager->addErrorMessage($e->getMessage());
            }

        }
    }
    }

<br>

system.log : <br>

    C:\xampp\htdocs\magento\var\log\system.log
<br>

    [2018-07-19 15:44:29] main.INFO: Item Magento_Backend::system_currency was removed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::system_design_schedule was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::system_store was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::dashboard was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::system was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::system_tools was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::system_design was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::system_convert was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::system_cache was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::marketing was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::marketing_communications was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::marketing_seo was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::marketing_user_content was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::content was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::content_elements was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::stores was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::stores_settings was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::stores_attributes was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::other_settings was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::system_other_settings was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backend::setup_wizard was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Fudu_HelloWorld::main_menu was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Fudu_HelloWorld::add_students was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Fudu_HelloWorld::manage_students was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Fudu_HelloWorld::configuration was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Theme::design_config was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Theme::system_design_theme was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Customer::customer was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Customer::customer_manage was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Customer::customer_online was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Customer::customer_group was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_AdminNotification::system_adminnotification was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Indexer::system_index was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Config::system_config was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Cms::cms_page was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Cms::cms_block was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Backup::system_tools_backup was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Catalog::catalog was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Catalog::catalog_products was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Catalog::catalog_categories was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Catalog::catalog_attributes_attributes was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Catalog::catalog_attributes_sets was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Catalog::inventory was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Search::search_terms was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Search::search_synonyms was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_UrlRewrite::urlrewrite was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Sales::sales was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Sales::sales_operation was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Sales::sales_order was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Sales::sales_invoice was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Sales::sales_shipment was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Sales::sales_creditmemo was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Sales::sales_transactions was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Sales::system_order_statuses was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Widget::cms_widget_instance was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_CheckoutAgreements::sales_checkoutagreement was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Downloadable::report_products_downloads was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_User::system_acl was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_User::system_acl_users was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_User::system_acl_roles was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_User::system_acl_locks was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Tax::sales_tax was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Tax::sales_tax_rules was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Tax::sales_tax_rates was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Email::template was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_CurrencySymbol::system_currency was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_CurrencySymbol::system_currency_rates was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_CurrencySymbol::system_currency_symbols was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Paypal::report_salesroot_paypal_settlement_reports was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Paypal::paypal_billing_agreement was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Integration::system_extensions was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Integration::system_integrations was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Amazon_Core::logs was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Amazon_Core::client_logs was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Amazon_Core::ipn_logs was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_CatalogRule::promo was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_CatalogRule::promo_catalog was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_ImportExport::system_convert_import was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_ImportExport::system_convert_export was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_ImportExport::system_convert_history was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Newsletter::newsletter_template was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Newsletter::newsletter_queue was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Newsletter::newsletter_subscriber was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Newsletter::newsletter_problem was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_EncryptionKey::system_crypt_key was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Braintree::settlement_report was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Analytics::business_intelligence was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Analytics::advanced_reporting was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Analytics::bi_essentials was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Marketplace::partners was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_SalesRule::promo_quote was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_marketing was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_salesroot was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_salesroot_sales was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_salesroot_tax was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_salesroot_invoiced was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_salesroot_shipping was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_salesroot_refunded was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_salesroot_coupons was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_shopcart_product was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_shopcart_abandoned was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_products was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_products_bestsellers was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_products_sold was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_products_viewed was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_products_lowstock was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_customers was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_customers_accounts was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_customers_totals was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_customers_orders was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_statistics was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Reports::report_statistics_refresh was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Search::report_search_term was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Review::catalog_reviews_ratings_ratings was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Review::catalog_reviews_ratings_reviews_all was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Review::report_review was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Review::report_review_customer was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Review::report_review_product was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Sitemap::catalog_sitemap was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_TaxImportExport::system_convert_tax was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Magento_Variable::system_variable was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::marketing_automation was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::automation_studio was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::exclusion_rules was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::marketing_automation_report was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::importer was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::automation was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::campaign was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::cron was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::dashboards was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::logviewer was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Dotdigitalgroup_Email::abandoned was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Shopial_Facebook::marketing_social was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Shopial_Facebook::shopial_facebook was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Temando_Shipping::shipping was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Temando_Shipping::carriers was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Temando_Shipping::locations was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Temando_Shipping::packaging was processed [] []
    [2018-07-19 15:44:29] main.INFO: Add of item with id Temando_Shipping::dispatches was processed [] []
12
  • You need API for this. Create custom api.
    – kunj
    Jul 19, 2018 at 13:29
  • what do u mean i need API for this ? i'm follow the module-cms in magento core project in the link below ~~~~~~~~~~~~ C:\xampp\htdocs\magento\vendor\magento\module-cms\Controller\Adminhtml\Page\Save.php ~~~~~~~~~~~~ can u make it clearer? thanks for reading my issue .
    – fudu
    Jul 19, 2018 at 13:32
  • and i do almost same thing as they do in that Save.php, so i dont know what u mean when u say i need API
    – fudu
    Jul 19, 2018 at 13:36
  • As you are calling admin side by CURL you need admin username and password second you can not call any controller without secure key. Magento have API feature to POST and GET data by CURL. I just review your updated code the execute method is magento own custom method that's why it is not calling and __construct is magic method it will default calling while call PHP file.
    – kunj
    Jul 19, 2018 at 13:37
  • hmmm ... but how can i make API to CRUD now ?
    – fudu
    Jul 19, 2018 at 13:40

3 Answers 3

6

Make your controller extend \Magento\Framework\App\Action\Action instead of \Magento\Backend\App\Action, that should solve your issue.

EDIT :

It could also be the case you're trying to hit your controller directly and "Add Secret Key to URLs" feature is turned on at Stores > Configuration > Advanced > Admin > Security > Add Secret Key to URLs. So basically it adds a key param in the URL which is always validated against the access to current resource. If the validation fails, it redirects to dashboard.

8
  • thanks alot, you r the boss , Kick . +1 vote for you my bro
    – fudu
    Jul 20, 2018 at 1:24
  • but can u explain why ? i'm follow the module-cms in magento project, i do the same as they do, but doesn't work. in C:\xampp\htdocs\magento\vendor\magento\module-cms\Controller\Adminhtml\Page\Save.php, they extends \Magento\Backend\App\Action, but i cant? why ?
    – fudu
    Jul 20, 2018 at 1:28
  • Tbh , I don't know the exact reason myself, it happened to me once and same thing solved my problem . I guess it was because I was not rendering any backend page, the controller was just for saving data to db which caused the redirection. Jul 20, 2018 at 2:03
  • Please do upvote the post if you want, I will look further into this when I get time... don't get much time from development to see what was causing the issue once it gets fixed. :p Jul 20, 2018 at 2:04
  • 1
    You can't grand permission for new Admin User on this controller, because when you extend Magento\Framework\App\Action\Action that mean by pass Magento Access Control list.
    – HoangHieu
    Jul 23, 2018 at 4:39
3

Try this code :

<?php

namespace Fudu\HelloWorld\Controller\Adminhtml\Student;

class Create extends \Magento\Backend\App\Action
{
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
        echo "controller call successfully";
        exit;
    }
}
3
  • doesn't work, i clear cache and gerenation folder as well
    – fudu
    Jul 19, 2018 at 15:43
  • Can you please upload system.log or exception.log here? Jul 19, 2018 at 15:48
  • system.log is updated on my post, please take a look
    – fudu
    Jul 19, 2018 at 15:54
3

You can't grant permission for new admin user on this controller, because when you extend Magento\Framework\App\Action\Action that mean by passing Magento Access Control list.

Please re-check these issues:

  • acl.xml file
  • make sure your you have _isAllowed function in your controller and return _authorization->isAllowed with resource id in acl.xml file.
  • Try to Log out and re-login admin user.

Ex Admin Controller.

Class [Action Name] extends \Magento\Backend\App\Action
{


    /**
     * {@inheritdoc}
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('[resource id in **acl.xml**]');
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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