简体   繁体   中英

getting the instance of controllers Joomla

So im trying to create my first component and for this ive created the following controller:

    <?php
defined('_JEXEC') or die ('Access denied!');
/**
 * Created by JetBrains PhpStorm.
 * User: Marc
 * Date: 18-08-13
 * Time: 02:18
 * To change this template use File | Settings | File Templates.
 */
jimport( 'joomla.application.component.controller' );
class MyCompController implements  JController{

    function create(){
        echo "Welcome to create";
    }

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

}

Now i am not very familiar with joomla and could only find documentation for 2.5 where

$controller = JController::getInstance('MyComp');

was a relevant way to get your controller instance.

i looked at the interface and JController interface does not supply a getInstance method because of this i tried:

$controller = JController::getApplication('MyComp');

But with no luck.

So how do you get the controller?

Your component has a bootstrap script with the same name as your component. In your case, that would be mycomp.php . That file is included by Joomla, whenever a request with option=mycomp occurs. There you setup and call your controller:

$controller = new MyCompController;
$controller->execute(JFactory::getApplication()->input->getCmd('task'));
$controller->redirect();

I'm not exactly sure what your trying to accomplish. But I think what you need is in the Joomla 2.5 tutorial .

You need to have a file called MyComp.php like they define HelloWorld.php in the link above.

That's it. Then when you create a view with some code, say a form:

<form action='index.php?option=com_MyComp&task=delete'>
....
</form>

This will access your controller's code for the delete function.

On another note, it may be easier to create the template you need for your component with Component Creator . Nice free online tool that builds the template, all the kids use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM