views:

240

answers:

3

I wrote a shell method in CakePHP 1.3 that has a return value.

I'd like to be able to access that method from within a controller, so that I can pass its return value into the View.

I'm not sure how to access those methods appropriately from within the controller. Have I done it wrong?

I could easily duplicate the code, but I'd like to "keep it DRY", and the actual functionality, I believe, doesn't belong with this particular controller - I just need it's return value in this particular view.

EDIT:

I realize I'm sort of asking the wrong question here, since the Shell itself shouldn't necessarily return a value. I've changed the code so that the Shell is only using the return value I want, and now I wonder - what is the appropriate place for extra classes/code that needs to be accessed from a Shell and a Controller?

It seems like Component code, but I'm not sure how to access Components from the Shell. It's not a Plugin, as I understand them. Where does this go?

A: 

Shells shouldn't directly return a value explicitly, they ought to report it somehow, e.g. by echoing it to stdout, logging to a file or sending an email for example. I like to think of shells as controllers for the cli.

Without knowing your application, my suggestion would be to see if you could refactor the logic in your current shell into a model class or something like that, have the model method return the value, then use that model in your shell. This way, you can also use that model in your controller.

neilcrookes
The Shell only uses the information returned from the important message to display it, so I guess technically it's not the Shell I need access to. Is there a proper place to put classes/code that needs to be accessed from a Shell and a Controller?
anonymous coward
A: 

You can create a component to do that.E.g

/* in app/controllers/components */
class ShellComponent extends Object
{
    function do_shell()
    {
       return shell_exec('some command');
    }
}

Then use it in any controller you want as below

/* in some controller*/
var $components = array('Shell','maybe some other components',....);

fucntion testShell()
{
    $result = $this->Shell->do_shell();
    ....
}
SpawnCxy
Since I like the idea of separating the code from the model, and as I mentioned - it's not directly related - a Component is a good idea. But can I access Components from a Shell?
anonymous coward
hmm that's not a good idea i think.components are for controllers in cake not shell in os.:)
SpawnCxy
A: 

In one of the projects we imported shell tasks, ex:


App::import('Core', 'Shell');
App::Import('Vendor','shells/tasks/sometask');

$returndata = TasknameTask::execute($somevalue);


openprojdevel