Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6184083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:28:30+00:00 2026-05-24T01:28:30+00:00

I have some component code that I’d like to in a CakePHP Task, but

  • 0

I have some component code that I’d like to in a CakePHP Task, but I’m not sure that I’m bootstrapping the Controller in the right way.

As a simplified version, I have two components that operate fine when accessed via the extended AppController. One component includes the other, because it calls the other.

The first component:

class BigBrotherComponent extends Object {

    var $Controller = null;

    var $components = array('Sibling');

    function startup(&$controller) {
        $this->Controller =& $controller;
    }

    function doThis() {
       $this->Controller->loadModel('SampleModel');
       $this->Sibling->doThat();
    }
 }

The second component:

class SiblingComponent extends Object {

    var $Controller = null;

    function startup(&$controller) {
        $this->Controller =& $controller;
    }

    function doThat() {
        /* Doing stuff */
    }
 }

To make my operate this from the command line, I define a Shell and a Task. This is the Task, though I’m not sure that I’m doing it right.

App::import('Core', 'Controller');
App::import('Component', 'Session');
App::import('Component', 'BigBrother');

class BigBrotherTask extends Shell {
    var $Controller; 
    var $BigBrother;

    function initialize() {
        $this->Controller =& new Controller();

        // add session to controller, because some components access $this->Controller->Session->setFlash();
        $this->Controller->Session =& new SessionComponent();
        $this->Controller->Session->startup($this->Controller);

        // Initialise the component
        $this->WordToText =& new WordToTextComponent();
        $this->WordToText->startup($this->Controller);
    }

    function doThat() {
        $this->BigBrother->doThat();
    }

}

This type of task works well when the BigBrother component does not include any of it’s own components, or reference any other components off the Controller in the BigBrother component. As you can see, I’ve had to do a slight hacky thing for the Session component.

Is there a better way to initialize and make use of components from tasks, that will properly initialize components and sub components?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:28:30+00:00Added an answer on May 24, 2026 at 1:28 am

    I believe I’ve come up with a better way, but still not sure if it’s the best way.

    Firstly, the components need to set the controller via the initialize() method. This ensures that the controller is assigned if the component is loaded via a controller or another component. startup() does not get run for components loaded by other components.

    Therefore, components should have the following

    function initialize(&controller, $settings = null) {
        $this->Controller =& $controller;
    }
    

    The next step to make make controller and component initialization a little more like what the dispatcher does.

    App::import('Core', 'Controller');
    
    class BigBrotherTask extends Shell {
        var $Controller; 
    
        function initialize() {
            $this->Controller =& new Controller();
            $this->Controller->components = array('Session', 'WordToText');
            $this->Controller->uses = null;
    
            $this->Controller->constructClasses();
            $this->Controller->startupProcess();
        }
    
        function doThat() {
            $this->Controller->BigBrother->doThat();
        }
    
    }
    

    Tasks don’t seem to have a shutdown as a part of their flow, so if your components need to do shutdowns properly, you’ll have to code that in at the end of the Task function, or in the Shell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I have some code that looks like this: import java.awt.BorderLayout; import java.awt.Component; import
I'd like to have a TabNavigator component that has a close button for some
I have a Flash component that's just a library of compiled code with some
I have some UI in VB 2005 that looks great in XP Style, but
I have some code that create a few components on the click of a
I have some c++ code that is a snap-in for a MMC based application.
I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
I have some repetitive code that is used by a few of my actions
I have inherited some code that will eventually be part of an API call.
I have written a new custom component derived from TLabel. The component adds some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.