How to load methods of a controller from another one? - php

I have the following controller:
class Tests extends CI_Controller {
public function update_record_test()
{
//some methods
}
}
?>
But I need to execute Method1 method from Controller1 controller. How can I do it?

There's not much information here, or I may be missing something, but what's wrong with
class Tests extends CI_Controller {
public function update_record_test()
{
$controller1 = new Controller1();
$controller1->Method1();
}
}

If you mean you want to execute a function of your main class CI_CONTROLLER, try this.
parent::Method1();
Check the http://php.net/manual/en/keyword.extends.php for more examples

Related

Laravel, is there a way to include codes from resource/view path into controller?

My goal is to include codes from another sources which is located in resources/views. I have tried using resource_path('views/myfiles.php') but it does nothing.
Controller
class MyController extends Controller
{
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$theFilesLocation = "resources.views" . $request->input('name');
#include($theFilesLocation) //something like this
}
}
}
myfiles.php
<?php
dump("if this shows up, then the code works")
?>
Try bellow code but I think it is not a good way.
class MyController extends Controller
{
require_one(resource_path('views/myfile');
}
Or with Laravel File facade
class MyController extends Controller
{
\File::requireOnce(resource_path('views/myfile');
}
You should create a class and put your code there then call it from the controller is a better solution.
What you are looking for is a trait. This allows the easy sharing of code and functionality without having to inherit from a specific base class causing an inheritance hell.
namespace MyCode\Traits;
trait SharedCodeForThing {
public function blaTheBla() {
dump("if this shows up, then the code works");
}
}
and then in your controller
use MyCode\Traits\SharedCodeForThing ;
class MyController extends Controller
{
use SharedCodeForThing;
}
Now if you wish to just render the contents of the view which it seems you're after:
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$view = view('resources.views' . $request->input('name'));
return $view->render();//or echo $view->render(); whatever you like
}
}

Codeigniter/PHP include re-useable functions in controller

I have this function that is constantly being reused in my controllers. I have decided to move it into a file that can be referenced all the time. This is my file structure
controllers
|Generic
|Users
|get_all_languages.php
|Users
|Lang
|Lang.php
I want to reference get_all_languages which contains
<?php
function get_all_languages(){
$this->curl->create(GetAllLanguages);
$this->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($this->curl->execute(),true);
}
So far, I have tried including it in the top of my file like:
<?php
include __DIR__.'/../../Generic/Users/get_all_languages.php';
class Lang extends CI_Controller{
However, when I try to use that function like $this->get_all_languages();, an error occurs saying Call to undefined method Lang::get_all_languages()
I have also tried to include it after the __contruct but it doesn't allow me to compile.
I hope someone can let me know how I can reference that function.
Thank you.
You can use library or helper of codeigniter.
And you can load them automatically in application/config/autoload.php.(Reference it)
If you want the specific controller, you can use it in construct of controller using $this->load->library() or $this->load->helper().
For example:
class A extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('libraryname');
$this->load->helper('helpername');
}
public function index() {...}
...
}
...
Updated
application/helpers/global_lang_helper.php
<?php
function get_all_languages(){
$CI = &get_instance();
$CI->load->library('curl');
$CI->curl->create(GetAllLanguages);
$CI->curl->http_login(REST_KEY_ID,REST_KEY_PASSWORD);
return json_decode($CI->curl->execute(),true);
}
At your controller...
public function __construct()
{
parent::__construct();
$this->load->helper('global_lang');
}

Accessing an extended class method from a sibling extended class (PHP OOP)

First off, I am relatively new to OOP as well as using an MVC, so I apologize if I do not use the right terminology or if I seem confused (because I am, haha)
I will start this off as basic as possible and if you need more information please let me know.
I am using Panique's MVC (Version HUGE)
https://github.com/panique/huge
So here goes nothing!
I have a base controller class that is setup like this...
Controller
<?php
class Controller {
public $View;
function __construct() {
$this->View = new View();
}
}
?>
With some extended controller classes like this (I will show two here)
IndexController
class IndexController extends Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->View->render('index');
}
}
?>
ProfileController
class ProfileController extends Controller {
public function __construct() {
parent::__construct();
}
public function profile() {
$this->View->render('profile');
}
}
?>
My Question is, what does it take (if at all possible) to use an extended class method within another extended class method when both have the same parent class. Something Like...
<?php
class ProfileController extends Controller {
public function __construct() {
parent::__construct();
}
public function profile() {
$this->IndexController->index(); //Here I would like to use the method from the IndexController
}
}
?>
I have tried many of attempts to make this work but I think my lack of knowledge using OOP is hindering me. It seems that most everything I try except for a few cases, throws an error of...
Fatal error: Class 'IndexController' not found in blah/blah/ProfileController.php
I think if I could learn to target the extended class the right way I could manage the rest...hopefully ;)
There's no easy or elegant way to do that. You would need to instantiate the other class inside the class that needs to borrow the code, and that would probably cause many side effects in your app.
There may be other ways to do that, and that also depends on the possibilities / limitations of the framework, but thinking from the perspective of OOP in PHP, ignoring other factors, the best approach would be to implement the shared code in a method on Controller class:
<?php
class Controller {
public $View;
function __construct() {
$this->View = new View();
}
protected function myCustomCode() {
...
}
}
?>
And then call it normally on descendents:
<?php
class IndexController extends Controller {
public function __construct() {
parent::__construct();
}
public static function index() {
$this->myCustomCode();
$this->View->render('index');
}
}
?>
<?php
class ProfileController extends Controller {
public function __construct() {
parent::__construct();
}
public function profile() {
$this->myCustomCode();
...whatever...
}
}
?>
I don't see a better way of doing that. Besides, this is the natural way of OOP, where common stuff is up on class hierarchy (ancestors), never sideways or down (descendents). That helps keeping your code logical and easier to maintain.
Include the file of the class IndexController:
require_once('IndexController.php');
$this->controller = new IndexController();
Then invoke the method
$this->IndexController->index();

codeigniter declaring a class in to one variable

i am trying to use $admin->function() instead $this->admin_model->function()
when i tried to declare a variable $admin=new Admin_model; in constructor and use it in other functions it gives error..
my code is given below, i don't know much about OOP concept, somebody please help.
class Admin extends CI_Controller {
var $admin;
function __construct() {
parent::__construct();
$this->load->model('admin_model');
$admin=new Admin_model;
}
public function index($value='')
{
if(!$admin->is_admin_logged_in()){
redirect('admin/login?r='.urlencode(current_url()));
}
$data['loggedin']=TRUE;
$data['account']=$this->session->all_userdata();
$this->load->view('pages/admin-home',isset($data)?$data:NULL);
}
}
presently i am using this method
public function login()// this function belongs to the same controller mentioned above
{
$r=isset($_GET['r'])?urldecode($_GET['r']):'admin';
$admin=new Admin_model;
if($admin->is_admin_logged_in()) redirect($r);
}
i don't want declare $admin=new Admin_model; in every single function and want to make the code look good and clean, so don't like to use $this->admin or $this->admin_model either.
You are trying to give an alias to a model (as what I can see)
All you have to do is:
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('admin_model', 'admin');
}
public function index($value='')
{
if(!$this->admin->is_admin_logged_in()){
redirect('admin/login?r='.urlencode(current_url()));
}
$data['loggedin']=TRUE;
$data['account']=$this->session->all_userdata();
$this->load->view('pages/admin-home',isset($data)?$data:NULL);
}
}
Notice where the alias of the model is given
$this->load->model('admin_model', 'admin');
And how it is used
if(!$this->admin->is_admin_logged_in()){

What is the correct way to access a model in an controller on Laravel

I'm new on Laravel 4 and I am trying to understand it.
is searched on google and on stackoverflow. Maybe i am not searching for the right syntax but i hope someone can help me, with it.
In CodeIgniter i understand it (probably). There I use in an Controller:
function __construct()
{ $this->load->model('example_m'); }
But how about in Laravel 4?
I figured out the following:
i make a static function in de model so i can access it everywhere. Example:
class Example extends Eloquent // this is the model
{
public static function TestExample(){
// do some stuff here
}
}
Or i could do it like this:
class ExampleController extends BaseController
{
public $test = null;
public function __construct()
{
$this->test = new Example();
}
public function index()
{
$this->test->TestExample();
}
}
My question is: Is there an other way and/or what is the correct way?
http://four.laravel.com/docs/ioc
App::bind('ExampleModelInterface', 'Example');
class ExampleController extends BaseController {
public function __construct(ExampleModelInterface $model)
{
$this->model = $model;
}
}
Do you mean simply accessing the method of a model?
Since they are static you use: Modell::method()
You might have to do a composer dump-autoload though so L4 autoloads it correctly.

Categories