Jump to content

What File Actually Calls The Plugin Handler?


gutterboy

Recommended Posts

I just made my very first plugin (very very simple at that haha) and it's basically a plugin that redirects the user after they logout. However I would like to be able to set a member variable for this action so I can pass in the URL I want to redirect to; I can do it via including a config file but I thought updating the member variable would be a better way.

 

How can I pass in a URL I want it to redirect to such as by setting the value of a member variable or even via a parameter that is passed to a constructor?

 

Thanks!

Link to comment
Share on other sites

The plugin is called by lib/dispatcher.php. I don't think it would be a good idea to modify that file to pass any variables. If you want to set a member variable you can declare it at the top of the class then initialise it within the constructor, optionally setting it's value to that from a config file.

class MyPlugin extends Plugin {
    private $memberVar;
    public function __construct() {
        $this->memberVar = "http://domain.com";
    }
    public function doSomething() {
        echo $this->memberVar;
    }
}
Link to comment
Share on other sites

Yeah, don't want to edit the core files or anything - just trying to figure out how this stuff is usually done.

 

For example this is my plugin handler code:

class LogoutRedirectPlugin extends Plugin {
	
    public $url;
	
    public function __construct() {
        $this->loadConfig(dirname(__FILE__) . DS . "config.json");
    }	
		
    public function getEvents() {
        return array(
            array(
                'event' => "Users.logout",
                'callback' => array("this", "run")
            )
            // Add multiple events here
        );
    }
 
    public function run($event) {
        header('Location: http://localhost/gimko_v2/');
    }	
	
}

If I was doing it normally I would just do something like this:

$logout = new LogoutRedirectPlugin();
$logout->url = 'http://www.mydomain.com/';

OR...

$logout = new LogoutRedirectPlugin('http://www.mydomain.com/');

...and then set it in the constructor.

Link to comment
Share on other sites

I haven't looked at the plugin architecture in Blesta but I don't think they are designed to be called manually. I think it would be better suited for a component, which would be called like this:

Loader::loadComponents($this, array("MyComponent"));
$this->MyComponent->redirect($url);

You may want to look at the built in redirect function Controller::redirect() in lib/controller.php

Link to comment
Share on other sites

I haven't looked at the plugin architecture in Blesta but I don't think they are designed to be called manually. I think it would be better suited for a component, which would be called like this:

Loader::loadComponents($this, array("MyComponent"));
$this->MyComponent->redirect($url);

You may want to look at the built in redirect function Controller::redirect() in lib/controller.php

 

Alright - thanks for your help!

Link to comment
Share on other sites

Only a plugin can react to events (this is dictated by the components/events/default/* files) but if you're wanting to call the redirect function from any file I think it should be in a component while the plugin handles the event, loads the component and performs the redirect. I don't think there are any docs on components, just look at a simple one like components/json. It's pretty much just a standard class.

Link to comment
Share on other sites

Only a plugin can react to events (this is dictated by the components/events/default/* files) but if you're wanting to call the redirect function from any file I think it should be in a component while the plugin handles the event, loads the component and performs the redirect. I don't think there are any docs on components, just look at a simple one like components/json. It's pretty much just a standard class.

 

Hmmm ok. Well I guess that leaves me with my initial problem then haha......... guess I'll have to include a config file or something.

Link to comment
Share on other sites

Any idea why when I change my method name to something else such as "redirect" I get a file doesn't exist error?

 

ie;

 

Change this:

    public function getEvents() {
        return array(
            array(
                'event' => "Users.logout",
                'callback' => array("this", "run")
            )
            // Add multiple events here
        );
    }
 
    public function run($event) {
        // code here
    }	

To this..

    public function getEvents() {
        return array(
            array(
                'event' => "Users.logout",
                'callback' => array("this", "redirect")
            )
            // Add multiple events here
        );
    }
 
    public function redirect($event) {
        // code here
    }	

Basically an error stating a file doesn't exist which comes from line: 120 of lib/view.php

Link to comment
Share on other sites

What page are you accessing? Does the error mention which file does not exist? The error is from the View::fetch() method so you need to find out which controller is calling that method and why it is passing an invalid file.

 

BTW to any Blesta devs reading this, line 120 of lib/view.php has a spelling mistake: "Files does not exist"

Link to comment
Share on other sites

When I clicked the log out button OR was redirected to the log out page via logging out on our main system is when that error showed.

 

Full error:

Files does not exist: D:\Website_Needs\CodeBase\Gimko_V2\billing\app\views\client\bootstrap\client_logout.pdt on line 120 in D:\Website_Needs\CodeBase\Gimko_V2\billing\lib\view.php

Printing Stack Trace:
#0 D:\Website_Needs\CodeBase\Gimko_V2\billing\lib\controller.php(277): View->fetch('client_logout', NULL)
#1 D:\Website_Needs\CodeBase\Gimko_V2\billing\lib\dispatcher.php(128): Controller->render()
#2 D:\Website_Needs\CodeBase\Gimko_V2\billing\index.php(21): Dispatcher::dispatch('/gimko_v2/billi...')
#3 {main}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...