How can I get current action?
This code:
if (!Yii::$app->controller->action->id == 'lang') {
Url::remember();
}
returns an error:
PHP Notice – yii\base\ErrorException
Trying to get property of non-object
You should use beforeAction() event instead of init().
Also you can simply use $this because it contains current controller.
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
if ($this->action->id == 'lang') {
Url::remember();
}
return true; // or false if needed
} else {
return false;
}
}
if use Yii2 in view - try this:
$this->context->action->id;
You can get current action name by:
Yii::$app->controller->action->id
And get the controller name with this one:
Yii::$app->controller->id;
Note: Remember that these will only work after the application has been initialized. Possible use: inside a controller action/ inside a model or inside a view
Reference: Class yii\web\Controller
You can get current action id by :)
Yii::$app->controller->id;
Related
I am working in Ionic3.
I have a function which calls another function from provider.ts and it returns an interface Object.
page.ts
getList(){
this.localdata = this.provider.getGlobalData();
}
provider.ts
getGlobalData(){
return this.gvData;
}
Now, any changes made to localdata are also changing gvData in provider.ts.
I don't want to copy the reference, just the value. How can I do it?
The following snippet will return a cloned version of your object.
provider.ts
getGlobalData(){
return JSON.parse(JSON.stringify(this.gvData));
}
You could also do it this way:
page.ts
getList(){
this.localdata = JSON.parse(this.provider.getGlobalData());
}
provider.ts
getGlobalData(){
return JSON.stringify(this.gvData);
}
Another idea is to use spread operator:
getGlobalData() {
return { ... this.gvData};
}
You can just do something like a Object.assing({},this.provider.getGlobalData()) to get a new copy of the object.
I have created an rest calling framework in YII2. In this I have a class APIRequest From this class I want to render a page if I get an error from API.
My code:
public static function response($response,$serviceObject)
{
if($serviceObject->responseCode == 420)
{
$errorMessage = $response->errorMessage;
return \Yii::$app->getView()->renderFile('#app/views/merchants/error.php',['errorMessage'=>$errorMessage]);
}
else
{
return $response;
}
}
But this is not working.
Not much information to go on. where is this method invoked? in the controller class?
perhapse this could help http://www.yiiframework.com/doc-2.0/guide-runtime-handling-errors.html
Generally the Yii REST API doesn't use any views. See
http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html
use
return \Yii::$app->view->renderFile('#app/views/merchants/error.php',['errorMessage'=>$errorMessage]);
instead of
return \Yii::$app->getView()->renderFile('#app/views/merchants/error.php',['errorMessage'=>$errorMessage])
I have been some time without programing in Synfony and I have some doubts.
Is posible that and Action Controller return a variable (for example and integer) instead of a Response Object or Json Object.
What I need is call a function inside another function in a different Controller. If the 2 functions live in the same Controller it has no problem (like this):
class AController{
public function AAction(){
$var = $this->BAction(); //Do whatever I want with $var
return Response ("Hello");
}
public function BAction(){
return 34; //return an integer instead of a Response
}
}
THE PROBLEM IS when the BAction is in another Controller. If I use a forward, Symfony expect that BAction return a Response object or a Json array, but I only want to return a simple variale.
Is this posible?? Return a simple integer...
Thanks a lot!!
No a Action must return a Response Object. But if you have two controllers (that will say two different classes) then you could create a service.
app/config/config.yml
services:
app.my_ownservice:
class: AppBundle\Services\OwnService
arguments:
entityManager: "#doctrine.orm.entity_manager"
app/Services/OwnService.php
namespace AppBundle\Services;
use Doctrine\ORM\EntityManager;
class OwnService {
/**
*
* #var EntityManager
*/
private $em;
public function __constructor(EntityManager $entityManager)
{
$this->em = $entityManager;
}
public function doSomething(){
// you could use the entitymanager here
return 'Okay i will do something.';
}
}
And from each controller (or whatever) you can do:
$myOwnService = $this->get('app.my_ownservice');
$text = $myOwnService->doSomething();
// echo $text;
A controller should never use another controllers action. Thats not the problem that Controllers solve. Symfony business logic structure is SOA based. (https://en.wikipedia.org/wiki/Service-oriented_architecture) Therefore for custom business logic you should always use either:
Services: http://symfony.com/doc/current/book/service_container.html
Events: http://symfony.com/doc/current/components/event_dispatcher/introduction.html
I made a file in library/My/Utils/Utils.php. The content of the file is :
class My_Utils_Utils{
public function test(){
$this->_redirect('login');
}
}
This class is called from a layout; the problem is with the _redirect(); I get this error : The page isn't redirecting properly. My question is how call the _redirect() function from a class made by you in ZEND framework 1 .
Thanks in advance.
Use redirect() instead of _redirect(). Usage is:
$this->redirect(<action>, <controller>, <module>, <param>);
In your case $this->redirect('login'); should do the trick.
You can use the redirector action-helper, which you can get statically from the HelperBroker using:
// get the helper
$redirectHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
// call methods on the helper
$redirect->gotoUrl('/some/url');
It should be noted, however, that the layout is considered part of the view layer. Typically, any checks that result in a redirect should probably take place earlier in the request dispatch-cycle, typically in a controller or in a front-controller plugin.
The _redirect function is provided by the class Zend_Controller_Action. You can fix this in two ways :
Extend Zend_Controller_Action and use _redirect
class My_Utils_Utils extends Zend_Controller_Action {
public function test(){
$this->_redirect('login');
}
}
in layout:
$request = Zend_Controller_Front::getInstance()->getRequest();
$response = Zend_Controller_Front::getInstance()->getResponse()
$util = new My_Utils_Utils($request, $response); // The constructor for Zend_Controller_Action required request and response params.
$util->test();
Use gotoUrl() function Zend_Controller_Action_Helper_Redirector::gotoUrl()
$redirector = new Zend_Controller_Action_Helper_Redirector();
$redirector->gotoUrl('login');
//in layout :
$util = new My_Utils_Utils();
$util->test();
I have a CodeIgniter application, but one of my controllers must call a data processing function that I have also written myself. The only problem is I can't seem to figure out how to do this. Looking through the user guide it seems that I should put my function inside the class declaration, and prefix it with an underscore (_) so that it cannot be called via the url. However, this is not working. Here's an example of what I mean:
<?php
class Listing extends Controller
{
function index()
{
$data = "hello";
$outputdata['string'] = _dprocess($data);
$this->load->view('view',$outputdata);
}
function _dprocess($d)
{
$output = "prefix - ".$d." - suffix";
return $output
}
}
?>
The page keeps telling me I have a call to an undefined function _dprocess()
How do I call my own functions?
Thanks!
Mala
Edit:
I've gotten it to work by placing the function outside of the class declaration. Is this the correct way of doing it?
This line is creating problem for you:
$outputdata['string'] = _dprocess($data);
Replace with:
$outputdata['string'] = $this->_dprocess($data);