Call controller function within another controller in Codeigniter - php

I am trying to call a function Commanfuntion in controller Return_test which is located in the Assign_test controller.
Example:
class Assign_test extends REST_Controller
{
function __construct()
{
parent::__construct();
}
function Commanfuntion()
{
//code
}
}
class Return_test extends REST_Controller
{
function __construct()
{
parent::__construct();
}
function Return()
{
//here I want to call Commanfuntion located in class Assign_test
}
}

As Codeigniter follows the MVC pattern you can't call a controller function in another controller. However you can do this by using a function helper and call the function from function helper in any controller.

Related

Why does my code end with: Call to a member function sample_template() on null

I am using hierarchical MVC model in codeigniter. I create a controller called template and inside it a function called sample_template. Then a view called sample_template_v created and call it inside the template controller. I create another controller called Admin and called Template->sample_template($data); inside its 2 funtions.
MY_Controlle.php
<?php
class MY_Controller extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->module('Template');
}
}
Admin.php
<?php
class Admin extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data['content_view'] = 'admin/admin_v';
$this->Template->sample_template($data);
}
function about()
{
$data['content_view'] = 'admin/about_v';
$this->Template->sample_template($data);
}
}
Template.php
<?php
class Template extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function sample_template($data = NULL)
{
$this->load->view('Template/sample_template_v', $data);
}
}
sample_template_v.php file---->
<h5>This is the main Template.</h5>
<?php $this->load->view($content_view); ?>
Error:
If you want to call a method from the object you need to initiaze the object and then call a method. Make sure $this->template is set in your case it isn't.
$this->template = new Template();
$this->template->sample_template($data);

In codeigniter how to get a reference to the child controller from a method in parent controller when that method is called from the child controller

Suppose I have two child controllers of MY_Controller,say Child_1 & Child_2.In MY_Controller I have two methods method_1 & method_2 as
abstract class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public abstract function method_1();//children will give custom implementation
public function method_2()
{
some code ...
/*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
}
}
I am calling method_2() from child_1 & child_2 using
class Child_1 extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function method_1(){//custom implementation goes here}
public function some_method()
{
...some code
$this->method_2();//call inherited method method_2()
}
}
A similar code for child_2
abstract class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public abstract function method_1();//children will give custom implementation
public function method_2()
{
// some code ...
$this->method_1(); //Call the method directly it will implement the child's method
/*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
}
}

How to access sibling controller function in PHP Codeigniter

I have the following controller structure:
with the following code:
MAIN CONTROLLER:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load_defaults();
}
public function load_defaults() {
}
}
CHILD CONTROLLER 1:
class Child1 extends MY_Controller {
public function __construct() {
parent::__construct();
$this->main();
}
public function main() {
echo "function in Child Controller 1";
}
}
CHILD CONTROLLER 2:
class Child2 extends MY_Controller {
public function __construct() {
parent::__construct();
$this->main();
}
public function main() {
echo "function in Child Controller 2";
}
}
My question: How do I call a function located in Child1 controller, from the Child2 controller?
If you have to call a controller from another controller, you're doing it wrong. Controllers are there to accept URI requests from the client.
Please try to revisit the problem and see if you can move the common logic to MY_Controller - it will be accessible because all other controllers extending it.
Also a model will be good place to have common functions that will be called from controllers.
I think what you are looking for is redirect. Considering that by CI convention each controller function should be an endpoint for the user, if you want to access a function from another controller then what you are really trying to do is redirect them to that controller.
function someEndPoint(){
$this->load->helper('url');
redirect('someOtherEndPoint', 'refresh');
}

PHP call zend call controller method in view file

My controller:
class CronController extends Zend_Controller_Action {
public function init(){
}
public function indexAction(){
die();
}
public function reportAboutExpiringPaymentAction(){
}
}
How can I call reportAboutExpiringPaymentAction() from file.phtml
The way that you need, doesn't work at all. Only way to call this function on a phtml is like this:
The URL is like: http://www.yoursite.com/cron/report-about-expiring-payment
And code is:
class CronController extends Zend_Controller_Action {
public function init(){
}
public function indexAction(){
die();
}
public function reportAboutExpiringPaymentAction(){
/* YOUR CODE HERE */
$this->render('file.phtml');
}
}
You can use View Actions helper to call an action form the view. In your case <?php echo $this->action('reportaboutexpiringpayment','cron',null); ?> will execute the action

How to use $layout in Laravel controller

I try to use the $layout property in my controllers, but I always get the following error:
Call to a member function nest() on a non-object
When dd() the value of this->layout, it seems like it is only a string.
Here's my code:
base.php
class Base_Controller extends Controller {
public $layout = 'layouts.common';
}
admin.php
class Admin_Controller extends Base_Controller {
public function action_index() {
$this->layout->nest('content', 'admin.home');
}
}
I probably miss something, but I can't find it in the docs
Ahhh, just found the trouble. I was ovewritting __construct to add filters, but wasn't calling the parent construct:
class Admin_Controller extends Base_Controller {
public function __construct() {
parent::__construct();
$this->filter('before', 'auth');
}
public function action_index() {
$this->layout->nest('content', 'admin.home');
}
}

Categories