Calling a method of a different controller from a controller

I got a question re calling a method of a different controller from a controller. I have seen this one: https://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

Using dispatcher forwarding, it doesn't produce any error. However, it doesn't call the method either. The code is similar to this one

        // Forward flow to the index action
        $this->dispatcher->forward(
            [
                "controller" => "posts",
                "action"     => "index",
            ]
        );

Am I missing something?

Thanks,

Note:

I am not doing any HTTP redirection. I just need to call a different method from another controller to do the task. These two are different in nature and at some point, the other controller has to use the other (think of it like an exposed method that can be reused by other controlelrs instead of duplicationg the code).

edited Oct '16

The best solution is to put this code from controller to service or to repository if it's only db related. Maybe you have some code on dispatcher which is preventing forward. Or you are trying forwarding to other module.

there is no any error ? What is happening then ? is this code even being executed ?

add a "use" statement at top of my controller file :


    use myOtherController as anyAlias;

This is the worst possible idea.

edited Oct '16

Do you have a dispatcher in your DI? Perhaps you're using Micro app? Then you don;t have a dispatcher.

Otherwise, It should work, no question about it.

why? cause it breaks the mvc pattern? it's inherent to class to be re-usable code...and a static method could be defined in the foreign controller if no instance need to be initialized..

This is the worst possible idea.

Because there are better ways to do this like forward(which should work) or put this code to some service.



3.9k

there is no any error ?

No, there is no error at all.

What is happening then ? is this code even being executed ?

Nothing happens. The method is not called at all. I placed an echo then exit() to see if it is called but it does not appear to be so.

What do you mean by putting the code from controller to repository? It is indeed db related. I have thought of calling the model directly and do the stuffs but that would mean duplicating the code and it is not the only one that will be using it in the future.

What I was aiming is to have an exposed method in that controller that can be called by other controllers as well. The process will be forwarded there but it has to return after it was done executing the script that belongs to it.

Thanks,

The best solution is to put this code from controller to service or to repository if it's only db related. Maybe you have some code on dispatcher which is preventing forward. Or you are trying forwarding to other module.

there is no any error ? What is happening then ? is this code even being executed ?



3.9k

Thanks for clarifying. I was wondering why is it not a good idea. Yes, I am looking into forward() but it happens to not work at all.

Because there are better ways to do this like forward(which should work) or put this code to some service.



3.9k
edited Oct '16

I am using MVC. No, I haven't set the dispatcher to DI. It wasn't mentioned in the link I provided above. Also, there was no error at all that the dispatcher I used was not defined, so I am assuming that it is existent... but is not since it is not called? I dunno.

If I need to set it to DI, how do I do that?

Do you have a dispatcher in your DI? Perhaps you're using Micro app? Then you don;t have a dispatcher.

Otherwise, It should work, no question about it.

UPDATE:

I have added the code below to add it on DI but still nothing. No error, not called. Same thing.

$di->set(
        "dispatcher",
        function () {
            return new MvcDispatcher();
        },
        true
    );
edited Oct '16

But what is happening ? What you mean not executed, try to put die() or exit() before this line where forward is happening. Is it stopped ? You have to have some problem in your code. For me forwarding is working without ANY problem in my application.

I mean if you have for example model User create UserRepository where you will have all queries, database related stuff etc. It's just some kind of pattern, instead of calling find, findFirst or creating query builder in controller just use repository class and do it there and in controller only call method from repository.

Just add some listener to disptacher to log beforeDispatch events to file, those for me is solving most of problems, as well exception logs etc.

@icewing02: Which OS, PHP and Phalcon version do you use?



3.9k

@ Jonathan Aaron Steel and Wojciech Ślawski:

Sorry for the late reply. I totally drop this notion already after several tries and moved over with a work around. Thanks a lot!

As for your suggestion Wojciech Ślawski, thanks. I am looking at it and trying to understand when would be the best time to use it and when to use the other. Re the die()/exit() thing, yes, I use it before and during the call, same issue. Will probably get back to this one sooner or later.

That's weird. Are you sure you're not using Micro app but full MVC?



3.9k

It's MVC.

That's weird. Are you sure you're not using Micro app but full MVC?