C
C
cat_crash2012-06-19 10:46:36
Yii
cat_crash, 2012-06-19 10:46:36

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

4 answer(s)
A
Alexey Prokhorov, 2012-06-19
@cat_crash

CController::forward + routing rules

D
d4rkr00t, 2012-06-19
@d4rkr00t

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();

S
Sergey, 2012-06-19
Protko @Fesor

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.

T
tnz, 2012-06-20
@tnz

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);

So what you need to do is something like this:
$request = Yii:app()->getRequest();
$request->pathInfo = $this->createUrl('/page/test', array('id'=>12));
$route=$this->getUrlManager()->parseUrl($request);
$this->runController($route);

But I'm not at all sure that you can set the pathInfo in the request this way, so it's even easier to
manually write parameters to $_REQUEST["id"]=$_GET["$id"]=12;
But it doesn't look kosher at all. It seems to me it will be more convenient to rewrite the UrlManager in your own way. It's just that if your requests are all processed in one controller, which in an unknown way knows where to forward what, then it's better to rewrite this stuff inside UrlManager-a. And through it, urls are formed for new pages (you don’t hard-code them, do you?). Once there, describe the rules or store them in the database or wherever necessary and that's it.
It is not very clear from the description of the problem... If you have several controllers Page, Product,... and there are urls like page 1 .html, product -cool .html respectively, then there is no problem. You just need to rewrite the routing rules for these controllers. Here is for Page for example
'page<id:\d+>'=>'page/index', //$id перейдет в indexAction($id)

For Product, you can also come up with
'product-<name:\w+>'=>'product/index', //$name перейдет в indexAction($name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question