【问题标题】:Getting values in joomla component controller function在 joomla 组件控制器功能中获取值
【发布时间】:2014-04-20 16:05:22
【问题描述】:

我面临一个问题(因为我对 Joomla 组件开发比较陌生)关于如何在我的 Joomla 自定义组件的 controller.php 中的两个函数之间传递值

下面是我的controller.php文件

 <?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controller library
jimport('joomla.application.component.controller');
JHtml::script(Juri::base() . 'templates/googlemaps/navigation.js');
/**
 * Hello World Component Controller
 */
class HelloWorldController extends JControllerLegacy
{
    function display(){
   echo '<form action="'. echo JRoute::_('index.php?option=com_helloworld&controller=helloworld&task=displayData') .'" method="post">';
   echo '<h1>This is Insert Task</h1><br><input type="text" id="name" placeholder="Name"><br>';
   echo '<input type="text" id="surname" value="surname" name="surname" placeholder="Surname">';
   echo '<input type="submit" value="submit">'; 
   echo '</form>';
    }

    function delete(){
        $id=JRequest::getVar('id');
        echo "You want to delete $id";
    }

    function displayData(){

        $name=JRequest::getVar('name');
        $surname=JRequest::getVar('surname');
        //$mainframe =& JFactory::getApplication();
        //$stateVar = $mainframe->getUserStateFromRequest( "$option.state_variable", "surname", "Hello" );
        echo "Your name is $name and surname is $surname";
        //**How can i get the name and surname variables here after the page refreshes and this task loads**
    }
}

我在控制器的 Display() 任务中有两个表单字段,我想在名为 displayData() 的另一个任务中获取我在这些字段中输入的值。 我想出的唯一方法是使用表单的 post 方法并通过 $_POST[ ] 方法获取变量。 Joomla 提供用户状态和变量以及所有这些东西,但我不知道如何在这里使用它们。

【问题讨论】:

  • 您在使用 1.5 吗?您是否有理由不能使用当前的 api?还有一个完整的 googlemaps 内置 api,你可能想看看。
  • 等等,你不能在 1.5 中,但你为什么要使用 JRequest?
  • 我正在使用 Joomla 2.5,很抱歉,但我想不出另一种方法在控制器(或其不同视图)中的两个任务之间传输数据,而不是 JRequest。关于 joomla 的 googlemaps 内置 api...你能给我提供任何相关的链接吗?
  • JRequest 已弃用,请改用 JInput。 docs.joomla.org/Retrieving_request_data_using_JInput。对于 API,它位于您的库文件夹中 libraries/joomla/google/embed/maps.php

标签: php joomla components


【解决方案1】:

首先我注意到你的表单确实有表单标签,因此它不能向服务器发送数据。更改代码如下。

    function display(){
       echo '<form action="'. echo JRoute::_('index.php?option=com_helloworld&controller=helloworld&task=displayData') .'" method="post">';
       echo '<h1>This is Insert Task</h1><br><input type="text" id="name" placeholder="Name"><br>';
       echo '<input type="text" id="surname" value="surname" name="surname" placeholder="Surname">';
       echo '<input type="submit" value="submit">'; 
       echo '</form>';       
    }

请注意,您可以使用 MVC 来提高清晰度。 MVC 是一种安排代码以便更好地开发和调试的形式。您可以创建以下结构。

  • com_helloworld

    • 控制器
      • helloworld.php
    • 型号
      • helloworld.php
    • 查看次数
      • 你好世界
        • view.html.php
        • tmpl
          • default.php

    在 com_helloworld->controllers->helloworld.php 下放这个。

     <?php
    // No direct access to this file
    defined('_JEXEC') or die('Restricted access');
    
    // import Joomla controller library
    jimport('joomla.application.component.controller');
    JHtml::script(Juri::base() . 'templates/googlemaps/navigation.js');
    /**
     * Hello World Component Controller
     */
    class HelloWorldController extends JControllerLegacy
    {
    
        function delete(){
            $id=JRequest::getVar('id');
            echo "You want to delete $id";
        }
        function displayData(){
            // LOAD THE MODEL AND DO PROCESSING THERE
            // $model = $this->getModel();
    
            $name=JRequest::getVar('name');
            //$surname=JRequest::getVar('surname');
            //$mainframe =& JFactory::getApplication();
            //$stateVar = $mainframe->getUserStateFromRequest("$option.state_variable", "surname", "Hello" );
            echo "Your name is $name and surname is $surname";
            //**How can i get the name and surname variables here after the page refreshes and this task loads**
       }
    
    }
    

在com_helloworld->view->tmpl->default.php下放这个。

    <form action="<?php echo JRoute::_('index.php?option=com_helloworld&controller=helloworld&task=displayData') ?>" method="post">
    <h1>This is Insert Task</h1><br><input type="text" id="name" placeholder="Name"><br>
    <input type="text" id="surname" value="surname" name="surname" placeholder="Surname">
    <input type="submit" value="submit"> 
    </form>       

【讨论】:

  • 感谢@Dedan 的指点...我已经按照您上面描述的方式成功了..抱歉发布的代码,我已经修改了它。我只是想确保这是在两个页面之间传递数据的正确和最佳方式。那么用户状态变量和所有这些呢?
猜你喜欢
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-18
相关资源
最近更新 更多