2016-12-06 47 views
1

保存數據I在路徑中創建模塊的應用程序/代碼/ Smartshore /認購Magento的2創建在前端的形式和實體

我想創建顯示形式的途徑和其保存的數據。我的代碼,但我不知道什麼是缺少:請參閱下面的代碼:

Smartshore /認購的/ etc /前端/ routes.xml

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> 
    <router id="standard"> 
     <route id="subscription" frontName="subscription"> 
      <module name="Smartshore_Subscription" /> 
     </route> 
    </router> 
</config> 

Smartshore /認購/控制器/索引/ index.php文件

<?php 

namespace Smartshore\Subscription\Controller\Index; 

use \Magento\Framework\App\Action\Action; 

class Index extends Action 
{ 
    /** @var \Magento\Framework\View\Result\Page */ 
    protected $resultPageFactory; 

    /** 
    * @param \Magento\Framework\App\Action\Context $context 
    */ 
    public function __construct(\Magento\Framework\App\Action\Context $context, 
           \Magento\Framework\View\Result\PageFactory $resultPageFactory) 
    { 
     $this->resultPageFactory = $resultPageFactory; 
     parent::__construct($context); 
    } 

    /** 
    * Subscription Index, shows a list of subscriptions 
    * 
    * @return \Magento\Framework\View\Result\PageFactory 
    */ 
    public function execute() 
    { 
     return $this->resultPageFactory->create(); 
    } 
} 

Smartshore /認購/控制器/索引/ Add.php

<?php 

namespace Smartshore\Subscription\Controller; 


use Magento\Framework\App\Action\Action; 
use Magento\Framework\App\Action\Context; 
use Magento\Framework\View\Result\PageFactory; 

class Add extends Action 
{ 
    protected $resultPageFactory; 

    public function __construct(Context $context, PageFactory $pageFactory) 
    { 
     $this->resultPageFactory = $pageFactory; 
     parent::__construct($context); 
    } 

    public function execute() 
    { 
     $resultPage = $this->resultPageFactory->create(); 

     return $resultPage; 
    } 
} 

的Sma rtshore /訂閱/控制器/索引/ Result.php

<?php 

namespace Smartshore\Subscription\Controller; 


use Magento\Framework\App\Action\Action; 
use Magento\Framework\App\Action\Context; 
use Magento\Framework\View\Element\Messages; 
use Magento\Framework\View\Result\PageFactory; 

class Result extends Action 
{ 
    /** @var PageFactory $resultPageFactory */ 
    protected $resultPageFactory; 

    /** 
    * Result constructor. 
    * @param Context $context 
    * @param PageFactory $pageFactory 
    */ 
    public function __construct(Context $context, PageFactory $pageFactory) 
    { 
     $this->resultPageFactory = $pageFactory; 
     parent::__construct($context); 
    } 

    /** 
    * The controller action 
    * 
    * @return \Magento\Framework\View\Result\Page 
    */ 
    public function execute() 
    { 
     $number = $this->getRequest()->getParam('number'); 

     $resultPage = $this->resultPageFactory->create(); 

     /** @var Messages $messageBlock */ 
     $messageBlock = $resultPage->getLayout()->createBlock(
      'Magento\Framework\View\Element\Messages', 
      'answer' 
     ); 
     if (is_numeric($number)) { 
      $messageBlock->addSuccess($number . ' times 2 is ' . ($number * 2)); 
     }else{ 
      $messageBlock->addError('You didn\'t enter a number!'); 
     } 

     $resultPage->getLayout()->setChild(
      'content', 
      $messageBlock->getNameInLayout(), 
      'answer_alias' 
     ); 

     return $resultPage; 
    } 
} 

Smartshore /訂閱/視圖/前端/佈局/ subscription_index_add.xml

<?xml version="1.0"?> 
<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> 
    <head> 
     <title>New Subscription</title> 
    </head> 
    <body> 
     <referenceBlock name="navigation.sections" remove="true" /> 
     <referenceContainer name="content"> 
      <block class="Magento\Framework\View\Element\Template" name="subscriptionform.add" template="Smartshore_Subscription::form.phtml"/> 
     </referenceContainer> 
    </body> 
</page> 

Smartshore /訂閱/視圖/前端/佈局/ subscription_index_result。 xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" 
     xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> 
    <head> 
     <title>Result</title> 
    </head> 
</page> 

我做了上述事情是否正確?

我還有其他文件。但我在這裏顯示了對我的表格及其提交的尊重。

當我輸入/訂閱/索引/加

我得到找不到路線。

什麼問題?

+0

嘗試訂閱/添加並檢查它是否打開?還要檢查你通過訂閱時會發生什麼? –

+0

當我通過/訂閱,我的Controller/Index/Index.php運行並顯示訂閱列表。當我打/訂閱/添加404頁面來 – anujeet

回答

1

檢查添加和結果文件/類中的命名空間。它應該是namespace Smartshore\Subscription\Controller\Index