class controller_page extends controller { public action_index() { $other_controller = new Controller_Cart_block(); $other_controller->anyMethod(); ........ } }
Valerio Riva wrote on Sunday 27th of November 2011:class controller_page extends controller { public action_index() { $other_controller = new Controller_Cart_block(); $other_controller->anyMethod(); ........ } }
class Controller_Welcome extends Controller { /** * The basic welcome message * * @access public * @return Response */ public function action_index($param1) { $widget = new Controller_Widget($this->request, $this->response); $widget->setSomething('This'); $widget->var2 = 'is an'; if ($param1 AND $widget->var2 == 'is an') { $widget->var3 = 'example'; } $data['widget'] = $widget->action_index(); return Response::forge(View::forge('welcome/index', $data)); } }
class Controller_Widget extends Controller { public $var1 = null; public $var2 = null; public $var3 = null; public function action_index() { return Response::forge(View::forge('welcome/index')); } public function setSomething($var) { /** * Some code here * * * */ $this->var1 = $var; } }
$widget = Request::forge('mycontroller/mymethod/parms')->execute();doesn't fit my needs because it's just a call of one method with some params and i can't use the 2nd controller as a full object. with "full object" i mean that i don't have to write widget method to act as a "function" when all parameters must be passed within the call of the method. with "full object" i mean that i can have the widget as an object that "lives" inside the caller. The example i wrote, actually works.. or well, it doesn't return errors or warnings. maybe there will be some conflicts between libraries (like database library or something else).. i don't know because i don't develop with fuelphp. What do you think about my solution? would be compatible with other components of fuelphp? thank you.
// store in app/classes/widget.php class Widget { public $var1 = null; public function __construct($request) { $this->request = $request; } public function render() { return Request::forge($this->request, array('widget' => $this))->execute(); } }
$widget = new Widget("mycontroller/mymethod"); $widget->var1 = 'blah'; $result = $widget->render();
// this works too $result = \Module\Controller_This::that($parms); // or this $object = new \Module\Controller_This(); $result = $object->that($parms);
Harro Verton wrote on Tuesday 29th of November 2011:The idea behind MVC, and in relation HMVC is abstraction, preferably combined with loose coupling. If you call class methods directly, you loose all that. You also loose the abstraction within the framework, if you modify something request related (for example search paths), you modify it for the entire request. HMVC requests also allow you to scale by converting an internal request to an external request, which means your original app becomes the web-tier, and your HMVC modules your application-tier, running on different servers.
Harro Verton wrote on Tuesday 29th of November 2011:You don't HAVE to do anything. You can do whatever you want. FuelPHP is flexible enough to accomodate you.// this works too $result = \Module\Controller_This::that($parms); // or this $object = new \Module\Controller_This(); $result = $object->that($parms);
namespace Substartmodule; use Fuel\Core\Request; use Fuel\Core\View; class Controller_Substartmodule extends \Fuel\Core\Controller { public function getSubMod( $d ){ if( Request::is_hmvc() ){ // HMVC $view = View::forge('top/subtop',$d); return $view; } else { Request::show_404(); } } }
namespace Decorator; use Request; class Controller_Decorator extends \Fuel\Core\Controller { public $height; public $width; public $name; public function action_test(){ $d = array( 'height'=>500, 'width'=>600, 'name'=>'Dsf' ); $obj = new \Substartmodule\Controller_Substartmodule(); $res = $obj->getSubMod($d); $this->response->body = $res; } }
4096! ErrorException [ 4096 ]: Argument 1 passed to Fuel\Core\Controller::__construct() must be an instance of Fuel\Core\Request, none given, called in V:\home\fuelphp\fuel\app\modules\decorator\classes\controller\decorator.php on line 27 and defined COREPATH/classes/controller.php @ line 35
I agree with you, but it is sometimes necessary to invoke methods on a single controller in the other. What do you think about this decision?Harro Verton wrote on Wednesday 4th of July 2012:You are right, they are also required. But having said that, someone who wants to instantiate controllers like this imho has some serious flaws in their application design. You should not do this, like you should not call one controller from another. Even though FuelPHP is flexible enough to actually let you do that.
namespace Testbase; class Controller_Testbase extends \Controller { public static $instance = null; public static function instance(){ if(!self::$instance){ self::$instance = new \Testbase\Controller_Testbase(); } return self::$instance; } public function getMe(){ return $hello = 'Me!'; } public function getAny($d){ return $d; } } namespace Test; class Controller_Test extends \Controller { public function get(){ \Module::load('testbase'); $obj = \Testbase\Controller_Testbase::instance(); $me = $obj->getMe(); $any = $obj->getAny('Oh yes)!'); $view = 'Hi! ' .$me .$any; return $view; } public function action_index(){ return $this->get(); } } \fuel\core\classes\controller.php - mod public function __construct(\Request $request, \Response $response) at public function __construct(\Request $request = null, \Response $response = null)
\fuel\app\modules\base\classes\controller\base.php \fuel\app\modules\base\views\indexbase.php namespace Base; class Controller_Base extends \Controller_Template { public $template = 'indexbase'; } \fuel\app\modules\module1\classes\controller\base.php \fuel\app\modules\module1\views\index.php namespace Module1; class Controller_Base extends \Base\Controller_Base { public function before($data = null) { parent::before(); $this->auto_render = false; } public function action_index(){ $this->template->content = \View::forge('index'); $this->response->body = $this->template; } } Error! Fuel\Core\FuelException [ Error ]: The requested view could not be found: indexbase COREPATH/classes/view.php @ line 381
public $template = 'Base::indexbase';
Harro Verton wrote on Friday 6th of July 2012:Interitance works fine. The issue here is that your $template is set in the Base namespace, but defined 'relative'. So your Module1 controller will search for it within it's namespace (as that is the current context) and if not found in app/views, but it will not search other modules. In this case you have to define the view as being located in the Base module:public $template = 'Base::indexbase';
It looks like you're new here. If you want to get involved, click one of these buttons!