component controller loading

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
User avatar
pishro
Joomla! Explorer
Joomla! Explorer
Posts: 301
Joined: Tue Oct 09, 2012 7:22 am

component controller loading

Post by pishro » Fri Sep 13, 2013 4:28 pm

component controller loading in Joomla! 2.5
I've been trying to learn how to build a Joomla component.

I've been using

Code: Select all

http://joomlaprogrammingbook.com
book which is great. I can now do plugins and modules without too much of a problem. However I'm getting stuck with how certain controllers get loaded for components. The site given has the full code if it's needed.

The initial controller loaded is:

Code: Select all

 class JoomproSubsController extends JController
{
    /**
    * @var      string  The default view.
    * @since    1.6
    */
protected $default_view = 'submanager';

/**
 * Method to display a view.
 *
 * @param   boolean         $cachable   If true, the view output will be cached
 * @param   array           $urlparams  An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
 *
 * @return  JController     This object to support chaining.
 */ 
public function display($cachable = false, $urlparams = false)
{
    JLoader::register('JoomproSubsHelper', JPATH_COMPONENT.'/helpers/joomprosubs.php');

    // Load the submenu.
    JoomproSubsHelper::addSubmenu(JRequest::getCmd('view', 'submanager'));

    $view = JRequest::getCmd('view', 'submanager');
    $layout = JRequest::getCmd('layout', 'default');
    $id = JRequest::getInt('id');

    // Check for edit form.
    if ($view == 'subscription' && $layout == 'edit' && !$this->checkEditId('com_joomprosubs.edit.subscription', $id)) {
        // Somehow the person just went to the form - we don't allow that.
        $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id));
        $this->setMessage($this->getError(), 'error');
        $this->setRedirect(JRoute::_('index.php?option=com_joomprosubs&view=submanager', false));

        return false;
    }

    parent::display();

    return $this;
}
I can see how and when this is loaded. However at some point in this it also seems to load class JoomproSubsControllerSubManager extends JControllerAdmin

now I though that to do this it would need a url which included com_joomproSubs?task=submanager

but that doesn't exist. So my question is how can this happen?

User avatar
pishro
Joomla! Explorer
Joomla! Explorer
Posts: 301
Joined: Tue Oct 09, 2012 7:22 am

Re: component controller loading

Post by pishro » Tue Oct 15, 2013 3:23 am

In Joomla! everything passes through index.php (although in the front-end you won't see index.php in the URL if the SEF options are turned on).

Without SEF etc

So the entry points are index.php or /administrator/index.php (for the backend). To this are appended some parameters that tell Joomla! how to route the request.

option=X
task=Y.Z
view=V
option

This is the parameter that tells Joomla what component is to handle the request. e.g. option=com_content for the component that handles standard articles. Subsequent to the component value being determined, Joomla is then expecting everything to be in the matching directory. In our example this will be /components/com_content/ or for the backend /administrator/components/com_content/

task

The task parameter can take a dot notation value of the form controller.method e.g. task=article.edit in the Article Manager. Using the elements of this value Joomla! loads the article.php file in the /com_content/controllers/ directory and fires the method edit

view

The view parameter is used to nominate a specific view within the same controller e.g. again in the com_content component you see these two variations in the value of view:

/administrator/index.php?option=com_content&view=featured

/administrator/index.php?option=com_content&view=articles

User avatar
Soren Jensen
Joomla! Explorer
Joomla! Explorer
Posts: 290
Joined: Fri Nov 11, 2005 8:53 am
Location: Granada, Spain
Contact:

Re: component controller loading

Post by Soren Jensen » Tue Oct 15, 2013 3:26 pm

The models, views and controllers in Joomla components are loaded automatically like so:

index.php?option=com_test

Will load a view called test by default and and no sub controller but it will automatically load the "test" model

index.php?option=com_test&view=test

Same as above

index.php?option=com_test&view=different

Will call the view different and the model different but still not use a sub controller

index.php?option=com_test&task=something

Will look for the method "something" inside your main controller

index.php?option=com_test&task=do.something

This will use a sub controller called do.php placed in the "controllers" folder and call the method "something" inside it.

You can try building a component using the Joomla Component Creator http://www.component-creator.com to see how it generates the code.
Soren Beck Jensenhttp://www.component-creator.com/ - Build Joomla Components fast and easy


Locked

Return to “Joomla! 2.5 Coding”