Answer the question
In order to leave comments, you need to log in
How to call a controller from another controller?
Historically, there are several controllers in a web application, such as:
pages
products
and default routing rules for them /
The customer expressed a desire to make all links SEO-friendly in the form /page1.html, /product-cool.html (that is, without specifying a controller) .
The obvious solution to the problem would be .htaccess but it was “wrapped” with the words no redirects. :(
The second thing that comes to mind is to make some kind of table of correspondences between the virtual address and the real controller, action and parameters. After that, to route all requests to this single “router2”, digging
through the documentation and did not find it - is it possible to call another in yii in one controller and display its output?
Answer the question
In order to leave comments, you need to log in
You can try to do this with routing rules, without one common controller. Controllers can be called like this:
list($controller) = Yii::app()->createController('site');
echo $controller->actionIndex();
Pretty trivial task. Solved by its URL manager. You just have to store somewhere a list of all urls and the corresponding route and parameters. There was such an extension, I remember. Used it about a year ago. But if necessary, this is written in an hour.
That won't work either.
in forward() CWebApplication::runController($route) is called without processing. And during the initial call, the url from the request is parsed, the parameters are entered in _REQUEST and all that:
$route=$this->getUrlManager()->parseUrl($this->getRequest());
$this->runController($route);
$request = Yii:app()->getRequest();
$request->pathInfo = $this->createUrl('/page/test', array('id'=>12));
$route=$this->getUrlManager()->parseUrl($request);
$this->runController($route);
'page<id:\d+>'=>'page/index', //$id перейдет в indexAction($id)
'product-<name:\w+>'=>'product/index', //$name перейдет в indexAction($name)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question