Let I've written the following action class:
class TranAction extends CAction{
public function run(){
echo "Hello Yii";
}
}
Now I want to render myView view, but I dont know how can I invoke controller's render() method from run() method?
$this->controller->render('myView');
Related
I got a problem on create a function that calling a function (index function). For ease of understanding let me explain it:
class My_Controller extends CI_Controller{
public function index(){
echo 'This is index function';
}
public function calling_index(){
// doing what's inside the index() function
}
}
Any suggestion ?
There won't be an issue in using
$this->index()
as the controller is simply a class
I have the following controller structure:
with the following code:
MAIN CONTROLLER:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load_defaults();
}
public function load_defaults() {
}
}
CHILD CONTROLLER 1:
class Child1 extends MY_Controller {
public function __construct() {
parent::__construct();
$this->main();
}
public function main() {
echo "function in Child Controller 1";
}
}
CHILD CONTROLLER 2:
class Child2 extends MY_Controller {
public function __construct() {
parent::__construct();
$this->main();
}
public function main() {
echo "function in Child Controller 2";
}
}
My question: How do I call a function located in Child1 controller, from the Child2 controller?
If you have to call a controller from another controller, you're doing it wrong. Controllers are there to accept URI requests from the client.
Please try to revisit the problem and see if you can move the common logic to MY_Controller - it will be accessible because all other controllers extending it.
Also a model will be good place to have common functions that will be called from controllers.
I think what you are looking for is redirect. Considering that by CI convention each controller function should be an endpoint for the user, if you want to access a function from another controller then what you are really trying to do is redirect them to that controller.
function someEndPoint(){
$this->load->helper('url');
redirect('someOtherEndPoint', 'refresh');
}
I have the following controller:
class Tests extends CI_Controller {
public function update_record_test()
{
//some methods
}
}
?>
But I need to execute Method1 method from Controller1 controller. How can I do it?
There's not much information here, or I may be missing something, but what's wrong with
class Tests extends CI_Controller {
public function update_record_test()
{
$controller1 = new Controller1();
$controller1->Method1();
}
}
If you mean you want to execute a function of your main class CI_CONTROLLER, try this.
parent::Method1();
Check the http://php.net/manual/en/keyword.extends.php for more examples
I have a custom MVC PHP framework that has a router class, which calls a controller, which uses a model, then the controller presents the view, etc etc.
My problem is that I can't figure out technically how to allow variables to pass between the controller and the view, semantically. I could do a quick-and-dirty fix, but what I want to have is this for a controller:
class IndexController extends Controller{
var $name = "John"; // instance variable
}
And have this for a view:
<p> <?=$name?> </p>
My question is this:
How can I create a Controller->render() function, or something similar, that allows the view to access instance variables from the controller? and,
How can I do this without doing klutzy things like $data['view']['name'] = "John"; or having to write ten lines of code by default for any new controller I make. I want to do this so it's as DRY as possible.
Thanks.
Edit: FabioCosta's solution
I'm not sure I understand, so far I have my base controller like this:
<?php
class Controller{
public function __get($key){
if(isset($this->$$key)) return $this->$$key;
}
}
?>
My base view class looks like this:
<?php
class View{
public $controller;
public function render(){
$this->controller = $this;
}
?>
And I initialize from the router like this:
<?php
$controller = new IndexController();
$view = new IndexView();
$view->render();
?>
However, this doesn't work, and I know I'm doing something wrong.
Why not pass the controller that instantiates the view and use the __get magic method?
like so:
public function __get($key){
if(isset($this->$key)) return $this->$key;
}
Here is a working example View.php:
class View{
protected $_controller;
public function __construct(Controller $controller){
$this->_controller=$controller;
}
public function render(){
echo '<h1>Hello '.$this->_controller->name.'</h1>';
}
}
Controller.php
class Controller{
protected $name='fabio';
protected $_myView;
public function __get($key){
if(isset($this->$key)) return $this->$key;
}
public function __construct(){
$this->_myView=new View($this);
}
public function indexAction(){
$this->_myView->render();
}
}
And the router:
$c=new Controller();
$c->indexAction();
Controller should not be responsible for rendering output. That is something view instances should do. Rendering should happen outside the controller.
View should request data from model layer. Then, based on information it received, select the right template, assign data and render this template (or in some cases - group of templates).
Also , router should not initialize neither controllers nor views. Controller should be responsible only for processing the request.
Is it possible in Zend View helper (extends Zend_View_Helper_Abstract) get info about module/controller/action in which that helper was called ?
Yes. You can use Zend_Controller_Front::getInstance() within view helpers. So you could do something like this:
class App_Helper_DoSomething extends Zend_View_Helper_Abstract
{
public function doSomething()
{
return Zend_Controller_Front::getInstance()
->getRequest()
->getControllerName();
}
}
Which will print the current controller name when called in your view with:
echo $this->doSomething();