Yii2 Basic calling a function from another file into controller - yii2

I create a file in yii2 "actions", and I create a class name actionC
is it possible to call function from
actions/actionC
inside a controller
my calss is
<?php
namespace app\actions;
class ActionC
{
protected function CPost(){
// return something
}
}
is it possible to call my function CPost() inside a controller actionView ?

Hello while your function is protected you can not call it, if you want to call a function of your class it has to be public it would be something like that
Class actionC
<?php
namespace app\actions;
class ActionC
{
protected function CPost()
{
// return something
}
public function BPost()
{
// return something
}
}
And in you View
<?php
$a=new \app\actions\ActionC();
$a->BPost();
//$a->CPost(); //this will be error because is protected

Related

Fetch data in codeigniter construct function

I try to fetch data from database in codeigniter construct function
but getting undefined method error
controller-
class Welcome extends CI_Controller
function __construct()
{
parent::__construct();
$this->load->model('Login');
$this->Login->getadminnav();
}
}
model-
class Login extends CI_Model
{
public function adminnav()
{
$query="SELECT * from adminnav where status='1'";
$query->row_array();
return true;
}
}
You got to load the model you are using first:
$this->load->model('login_model');
$this->Login->getadminnav();
And remember to rename the model to Login_model
Try these pieces of code.
Controller:
class Welcome extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('Login');
$this->Login->adminnav();
}
}
Model:
class Login extends CI_Model{
public function adminnav(){
$this->load->database();
$sql = "SELECT * from adminnav where status='1'";
$result = $this->db->query($sql);
return $result;
}
}
I am seeing in your code that-
In controller you didn't start the second bracket after controller name.
You defined the function name as "adminnav" in model but called it as "getadminnav" in Controller
You didn't load the database in model (If you already loaded it in autoload.php then here is not needed)
Anyway please let me know whether your problem is solved or not.
you are calling getadminnav() but in your model the method name is adminnav.and change model name Login to Login_model.
try this:
class Welcome extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('Login_model');
$this->Login->adminnav();
}
}
// model
class Login_model extends CI_Model{
public function adminnav()
{
$query="SELECT * from adminnav where status='1'";
$query->row_array();
return true;
}
}

In CakePHP3.6, how to call function of another controller into other controller?

I am using CakePHP3.6 for my project. I have created following function in Options controller:
public function getValue($id = null){
$options=$this->Options->find()->where(['id'=>$id])->first();
return $options->value;
}
then I am calling this function into function of another controller(any xyz controller) as follow:
$r = $this->Options->getValue(1);
Now I am getting error 'Unknown method "getValue"'.
Same Procedure I followed for CakePHP3.2, it worked well. So Please help me, How to call function of another controller into function of other controller for CakePHP3.6?
You should never be calling a function from one controller in another, and I doubt very much that this worked as shown in 3.2. This function looks very much like it should be in the OptionsTable, not OptionsController. The only change you'd need to make is inside the function body, it will just be $this->find(), not $this->Options->find().
class FirstController extends AppController
{
public function getValue($id = null){
$options=$this->Options->find()->where(['id'=>$id])->first();
return $options->value;
}
}
class SecondController extends AppController
{
public function anotherAction($id = null){
$anotherController = new \App\Controller\FirstController(); 
$r = $anotherController->getValue($id) ;   
}
}

how to get another function in another controller (Codeigniter)

i made controller name crud.php this the code :
class Crud extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('m_data');
$this->load->helper('url');
}
public function admin(){
$data['produk'] = $this->m_data->tampil_data()->result();
$this->load->view('v_admin',$data);
}
}
i want take the admin() to place in admin.php code here :
class Admin extends CI_Controller{
function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
function index(){
$this->crud->admin();
}
}
what's code for take another function in another controller.. that's possible or not?
You could try to require Crud controller within Admin controller :
public function index()
{
require_once('crud.php');
$crud = new crud();
$crud->admin();
}
I do not know if I understood your question very well, but you could have the controller Admin extend the Crud controller, so he would have access to the admin ()
class Admin extends Crud{
function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
function index(){
$this->admin();
}
}
Loading another controller inside a controller is a bad idea. It can lead to hard to find bugs. One solution is to make Crud a custom library that the controller loads.
File: /application/libraries/Crud.php
class Crud
{
protected $CI;
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->model('m_data');
$this->CI->load->helper('url');
}
public function admin()
{
$data['produk'] = $this->CI->m_data->tampil_data()->result();
$this->CI->load->view('v_admin', $data);
}
}
Then the controller loads and uses the custom class.
class Admin extends CI_Controller{
public function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
public function index()
{
$this->load->library('crud');
$this->crud->admin();
}
}
You could load crud in the constructor too. That would make sense if other methods in the controller use the library.
May it can be useful for you, if you will customize your code in the view:
VIEW
your-link-to-another-controller

How register events in yii2

I would like to trigger some events in my model, if I register my events via app->on in index.php then I don't receive required $event->data from event object.
How register my events handlers, so when I call trigger it will call correct function with event object.
Code sample:
namespace app\modules\Product\models\Product;
class Product extends Entity\Entity {
public static function tableName() {
return 'sometable';
}
public function afterFind() {
parent::afterFind();
$event = new yii\base\Event;
$event->data = $this;
$this->trigger('calculatePrice', $event);
}
}
namespace app\modules\PriceRules\models;
class PriceRules extends yii\base\Model {
public static function calculatePrice($event) {
$entity = $event->data;
}
}
//somehow register events
Add in Product in method init this code:
class Product extends Entity\Entity {
public function init()
{
$this->on('calculatePrice', ['app\modules\PriceRules\models\PriceRules', 'calculatePrice']);
}
...
}
See doc - http://www.yiiframework.com/doc-2.0/guide-concept-events.html
I found solution. I placed PriceRules module inside bootstrap and added this code there:
yii\base\Event::on(Product::className(), 'calculatePrice', [PriceRulesEvent::className(), 'calculatePrice']);

Calling a function from within that class - php

I have a function called wsGO() inside a class ceff(). I want to be able to run that function from within the class ceff. I used this code:
class ceff{
$ceff_instance = new ceff($this);
$ceff_instance = wsGO();
public function wsGO(){
....
}
}
However this didn't work. Any ideas?
In PHP you can only initialize a property with a constant - NOT functions or constructor calls. You can though do that in a constructor, such as:
class ceff{
public $ceff_instance;
public function __construct() {
$this->ceff_instance = $this->wsGO();
}
public function wsGO(){
....
}
}