0

i want inside a controller and within the function init(){} to change the called action (requested action). I mean if someone calls "www.mywebsite.com/myctrl/action1" i want inside the init function to call action2 instead, without redirecting the page and changing the url.

2- Is it possible to make a response inside init function, and stop calling the requested action ?

Best Regards Wael

Wael
  • 455
  • 1
  • 4
  • 17
  • possible duplicate of [Forward data from one controller action to other in yii2](http://stackoverflow.com/questions/28251890/forward-data-from-one-controller-action-to-other-in-yii2) – topher May 27 '15 at 12:02
  • i tried already Yii::$app->runAction('new_controller/new_action', $params); but do you know how to call it with a modules, a i mean if your code is working as a module: "www.mywebsite.com/mymodule/myctrl/action1 – Wael May 27 '15 at 12:04
  • and the mentioned question is not solved also. – Wael May 27 '15 at 12:05
  • it may be that the OP hasn't accepted it. That doesn't mean the answer is wrong. – topher May 27 '15 at 12:07
  • Are your controllers in the same module? – topher May 27 '15 at 12:09
  • yes, it's in the same controller – Wael May 27 '15 at 12:14

2 Answers2

0

Why not just catch the URL in your config file and manually redirect it to the new action? It should work just fine and it would accomplish exactly what you want.

http://www.yiiframework.com/doc-2.0/guide-runtime-url-handling.html

I know this is not the way you wanted to do it but what is the advantage.

EDIT:

After you told me what you need to do then you really should not be doing this in this way. You should either be using AccessControl http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html or the beforeAction http://www.yiiframework.com/doc-2.0/yii-base-controller.html#beforeAction()-detail of the controller.

With the second option you can just throw an error and let Yii take care of the handling of it for you. The first one does kind of the same thing...

Mihai P.
  • 9,307
  • 3
  • 38
  • 49
  • i am writing this module as API, so i want to show specific error message when the request not authenticated. – Wael May 28 '15 at 05:15
  • see my edit, do it with access control or beforeAction. Throw exceptions that is what they are there for. – Mihai P. May 28 '15 at 05:34
  • Thank you, i find beforeAction is elegant, with ErrorHandling. It helps actually now, but it is not possible to call another action through the execution without redirecting the page ? – Wael May 28 '15 at 09:07
  • I did do it at 1 point, but quickly removed it. it just felt like an ugly hack. You have to change the controller id in yii app... and some other things that were simply not needed. – Mihai P. May 28 '15 at 22:09
0

This problem is usually solved by using Apache's mod_rewrite or similar. There is no need to change your application at all. It's much more flexible than what yii2 has to offer and it'll be quicker because the web server is doing it natively.

The technique is generally used when migrating features, supporting different environments, sharing resources etc.

David Newcomb
  • 10,639
  • 3
  • 49
  • 62