0

In my system plugin, I use the following condition

class PlgSystemMyPlugin extends JPlugin
{
    public function onAfterDispatch()
    {
        $uri = JFactory::getURI();
        $current_url = $uri->toString();
        if(strpos($current_url,'?mykey') === false) {
            return true;
        }
    // some event that is being executed when url HAS ?mykey
    }
}

It works perfectly. But I need one more event that will be executed under reversed condition i.e. when url has no ?mykey

I try

class PlgSystemMyPlugin extends JPlugin
{
    public function onAfterDispatch()
    {
        $uri = JFactory::getURI();
        $current_url = $uri->toString();
        if(strpos($current_url,'?mykey') === false) {
            return true;
        }
        // some event that is being executed when url HAS ?mykey

        if(strpos($current_url,'?mykey') !== false) {
            return true;
        }
        // some event that is being executed when url HAS NO ?mykey
    }
}

but it doesn't work and it looks like an incorrect statement

2

Seeing as the URL can either have or not have ?mykey, surely you can use an else condition can't you?

Like so:

if(strpos($current_url,'?mykey') !== false) 
{
    // URL has ?mykey

    // Do some fancy code here

    // You've finished what you need to do, so stop anything else
    return true;
}
else
{
    // URL doesn't have ?mykey

    // Do some fancy code here

    // You've finished what you need to do, so stop anything else
    return true;
}
  • Yeah, I know about it. But if you'll look in Joomla loadposition plugin (lines about 32-34) there is reversed condition the events are being executed under. I can't understand why, maybe because of return true. I'm new in PHP and don't know what it means but in plugin(s) for some reason it works under reversed condition – stckvrw Sep 22 '15 at 17:45
  • @stckvrw - the return statement immediately ends execution of the current function. So if you're using it before you need to process any code, it won't work. I've update my answer for you – Lodder Sep 22 '15 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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