How can I call a specific function in my controller - function

I'm trying to create a module.
On my Dashboard I have a table with all my elements in my database, but, I don't know how I can set a function present in my controller in my smarty template.
Example of my function in my AdminController and who extends ModuleAdminController
public function deleteAction($id)
{
//here my logic
}
In smarty how can I set my link to redirect to my function?
Delete

Try to set your method with public static scope and then call it from you tpl. Something like
public static function deleteAction($id)
{
//do semething here
}
and then call it from tpl like
Delete
But be aware that it would work only from related tpl but not everywhere

Related

ASP .Net Core Razor: Can't return ViewComponent from my PageModel

I am trying to use Ajax to call a handler in my Razor page that returns the result of a ViewComponent, however when I try the code below, it says:
Non-invocable member "ViewComponent" cannot be used like a method.
public IActionResult OnGetPriceist()
{
return ViewComponent("PriceList", new { id= 5 });
}
When using MVC, the Controller base class includes a ViewComponent method, which is just a helper method that creates a ViewComponentResult for you. This method does not yet exist in the Razor Pages world, where instead you use PageModel as the base class.
One option to work around this is to create an extension method on the PageModel class, that would look something like this:
public static class PageModelExtensions
{
public static ViewComponentResult ViewComponent(this PageModel pageModel, string componentName, object arguments)
{
return new ViewComponentResult
{
ViewComponentName = componentName,
Arguments = arguments,
ViewData = pageModel.ViewData,
TempData = pageModel.TempData
};
}
}
Apart from it being an extension method, the code above is just ripped out of Controller. In order to use it, you can call it from your existing OnGetPriceList (typo fixed) method, like this:
public IActionResult OnGetPriceList()
{
return this.ViewComponent("PriceList", new { id = 5 });
}
The key to making it work here is to use this, which will resolve it to the extension method, rather than trying to invoke the constructor as a method.
If you're only going to use this once, you could forego the extension method and just embed the code itself inside of your handler. That's entirely up to you - some people might prefer the extension method for the whole separation-of-concerns argument.

How to use renderAjax() directly?

I create helper without extends controller, like this
class SomeClass{
public function static SomeMethod(){
//this function for send email
$controller = new \yii\web\Controller();
$controller->renderAjax("template", "array");
}
}
how ever the above code generate Missing argument 1 for yii\base\Controller::__construct(), called in common\component\BeoHelper.php on line 1715 and defined
How I can fix it?
Try this:
\Yii::$app->controller->renderAjax(...);

Display function in header.tpl - Prestashop

I would like to say that i am quite new to prestashop.
I have added FrontController.php to override/classes/controller.
The file contains the following code
class FrontControllerCore extends Controller
{
public function thefunct()
{
return 'AA';
}
}
Now in the header.tpl i try to call the function by using {FrontController::thefunct()}.
When I put the public function in classes/controller it works, but when I put it in the override folder it doesn't.
How do I display the function in my header.tpl?
And how does the override folder work then?
(I assume you use ps 1.5)
If you are overriding FrontController you need to initiate your class like so
class FrontController extends FrontControllerCore
and delete cache/class_index.php file, (I mean, delete it everytime you override new controller or class)
Teach yourself how to override in ps. through excellent docs.
EDIT:
class FrontController extends FrontControllerCore {
//here you should create and assign variables which you want to use in templates
public function initContent()
{
parent::initContent();
//in such a way you assign variables into smarty template
$this->context->smarty->assign(
array(
'acme_variable' => $this->acme()
)
);
}
protected function acme()
{
return "Wile E. Coyote and The Road Runner";
}
}
//header.tpl
<div>{$acme_variable}</div>

Actionscript : Variable not found from another class' function

I tried doing
trace(classname.functionname.variablename);
//or
trace(classname.functionname().variablename);
Didn't work.. any idea, to get from the classname.as the variable, that's inside a function?
Btw i tried making the function static, still didn't work
Any idea?
There's no way, as those variables that are defined inside a function only live as long as the function is executed, and disappear once there's a return or end of function body. In order to get whatever value you want from a function, make a class variable outside the function, assign it the value you want within that function, and address it from elsewhere.
class test {
public static var foo:Number;
function bar():void {
// ... some code
foo=baz*2.54;
// ... more code
}
}
class elsewhere {
...
trace(test.foo);
...
}
the variables created inside a function are only available in the scope of that function.
if the variables are class member variables (declared public on a class);
public class x {
public var varName:String="";
}
you will be able to access them as
classInstanceRef.varName
needless to say you will need to instantiate from that class an instance.
Unless your variable is declared static on the class
public static varName:String="";
and in that case you can access it using
className.varName;

Set Header as 'application/json' to all actions in controller YII

I am creating a rest api using yii framework so the basic output format would be json....
I want all actions in a controller to have header content-type as 'application-json'.
I tried to put it in beforeFilter function in the controller but it didn't work.
Can any one help me out...
Create an init() function in the Controller class (protected/components/Controller.php). This will be called when any Controller/Action is called. Eg:
public function init(){
if ($this->id == 1){
// perform controller specific function
}
}
The $this->id returns the controller id. You must obviously replace the 1 in the code above with the relevant controller id for the controller you want the function to take place with