I want to load a function from another controller. This is my structure:
- modules
--orderpages
---controllers
----WebshopCore.php
----WebshopController.php
My function insertItemInCart in WebshopController.php is called. But when i want to execute a function from another controller it crashes.
class WebshopController extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->module('orderPages/WebshopCore');
}
function insertItemInCart(){
$partId = $this->input->post('partId');
$quantity = $this->input->post('quantity');
$output = $this->WebshopCore->getPickLocations($partId,$quantity);
}
}
My WebshopCore:
class WebshopCore extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function getPickLocations($partId,$amount){
$result = "test";
return $result;
}
}
What goes wrong? I don't get it
The solution:
$output = modules::load('orderPages/WebshopCore/')->getPickLocations($partId,$quantity);
You should write a library in that case.
In application/libraries create Cart.php (or whatever you want)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cart
{
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
}
public function getPickLocations($partId, $qty)
{
//Your stuff
}
}
And then in your controllers :
$this->load->library("cart");
$data = $this->cart->getPickLocations($this->input->post('partId'), $this->input->post('quantity'));
Related
I need function (literally i need the $data) from this model in whole project.
when i call in some controller $data = $this->multi_language_model->multi_lang(); it work fine. But is it possible to call in one place, so I can use in every controller and view.
I autoload the model
$autoload['model'] = array('multi_language_model');
class Multi_language_model extends MY_Model
{
public function __construct() {
parent::__construct();
}
public function multi_lang() {
$data['menu_delivery'] = $this->lang->line('menu_delivery');
$data['menu_quotations'] = $this->lang->line('menu_quotations');
$data['menu_customer_service'] = $this->lang->line('menu_customer_service');
return $data;
}
}
Put this helper function in your any loaded helper:
function get_multi_lang(){
$CI = & get_instance();
$data = array();
$CI->load->helper('language');
$CI->lang->load('menu','english');
$data['menu_delivery'] = $CI->lang->line('menu_delivery');
$data['menu_quotations'] = $CI->lang->line('menu_quotations');
$data['menu_customer_service'] = $CI->lang->line('menu_customer_service');
return $data;
}
Controller:
class Yourclassname extends CI_Controller {
var $menu_delivery = "";
var $menu_quotations = "";
var $menu_customer_service = "";
function __construct() {
parent::__construct();
$data = get_multi_lang();
$this->menu_delivery = $data['menu_delivery'];
$this->menu_quotations = $data['menu_quotations'];
$this->menu_customer_service = $data['menu_customer_service'];
}
public function index(){
echo $this->menu_delivery.'<pre>';
echo $this->menu_quotations.'<pre>';
echo $this->menu_customer_service.'<pre>';die;
}
}
If you dont want to use helper function then copy helper function lines in controller __construct() [replace $data to $this->]so direct use global variables do same in model and for view pass this variable via controller
I don't know how codeigniter works but maybe you could do this method static and use it like:
class Multi_language_model extends MY_Model
{
private static $data = [];
public function get_multi_lang()
{
return [
'menu_delivery' => $this->lang->line('menu_delivery'),
'menu_quotations' => $this->lang->line('menu_quotations'),
'menu_customer_service' => $this->lang->line('menu_customer_service'),
];
}
public static function multi_lang()
{
if (empty(self::$data)) {
self::$data = (new self)->get_multi_lang();
}
return self::$data;
}
}
Then, whenever you need it, you can use $data = Multi_language_model::multi_lang();
However, I don't see anything wrong in injecting it from the container, wherever you need it. Doing so would be much easier to build tests.
BTW you don't need to overwrite the class constructor if there are no custom parameters set to the extending class. You can safely remove:
public function __construct() {
parent::__construct();
}
When I load a library in CI controller the class constructor is activated automatically, even if I didn't create an object just yet.
This is weird. can this be fixed via config or something? didn't find anything on the web.
My Controller:
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('users',false);
$user = new Users();
$this->load->model('welcome_model');
$this->load->view('welcome_message');
}
}
My Class:
defined('BASEPATH') OR exit('No direct script access allowed');
class Users {
public function __construct($uname, $fname, $lname) {
print "Hello World!";
}
public function some_method() {
}
}
The code will output "Hello World" twice.
$this->load->library('myLib'); instanciate the library, so you don't need to use the new keyword.
$params_for_the_constructor = array(
'id' => 1,
'firstname' => 'Jemmy',
'lastname' => 'Neutron'
);
$this->load->library('users', $params_for_the_constructor);
$this->users->some_method();
class Users {
private $id;
private $firstname;
private $lastname;
public function __construct(array $params = array()) {
if (count($params) > 0) {
$this->initialize($params);
}
log_message('debug', "Users Class Initialized");
}
public function some_method() {
}
public function initialize(array $params = array()) {
if (count($params) > 0) {
foreach($params as $key => $val) {
if (isset($this->{$key}))
$this->{$key} = $val;
}
}
return $this;
}
}
https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
in Codeigniter when you call library or call helper or call a controller or a model or anything of these its just equivalent to the new key word.
i mean if you want to make a new object of user you just do the following.
just store the instance of your first user in a varaible.
//creating first instance
$user = $this->load->library('users', $first_user_parameters);
//creating second instance
$user2 = $this->load->library('users', $second_user_parameters);
// $this->load->library('users', $param) === $user = new User($param);
in codeigniter they convert the load to new keyword by the ci_loader
I have this code and i´m trying to use a object
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
?>
i would like to do this:
class Index extends Controller {
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
i would like to use the set_parms function.
but i can't see the View Function, then i can not use.
Can someone explain and advise me a good and safe way?
Correction from Phil: If a __construct() method isn't found, PHP will revert to legacy constructor syntax and check for a method with the same name as the object. In your case the method index() is being treated as the constructor, and is preventing the parent's constructor from loading the view object into the $_view property.
You can force a class to inherit a parent's constructor by defining __construct() in the child and calling the parent's constructor:
public function __construct() {
parent::_construct();
}
Here is the fixed code:
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
.
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
.
class Index extends Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
I have this controller in Code Igniter application. A value is initialized in the constructor.
class Cat extends CI_Controller {
private $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $data);
}
}
The view "views/myCatPage.php" looks like this. It is simple.
<?= $sound ?>
Why does PHP note this error?
Message: Undefined variable: sound
I thought I sent this variable as a key in the array ($data) I sent into the view.
I have tried
$this->load->view('myCatPage', $this->data);
but that strangely fails too.
class Cat extends CI_Controller {
var $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $this->data);
}
}
In the index file i have _autoload and load the libs and then i explode the url to get the wanted contoller and the model if exists. In the view i can see the model __construct() so the model is loaded but if i try to use $this->model->test(); i get
Call to a member function test() on a non-object
http://site.com/about
$this->request = about;
$controller = new $this->request;
$controller->loadModel($this->request);
Everething work ok
*Here is the Main controller *
class Conroller {
function __construct() {
// echo 'Main controller<br />';
$this->view = new View();
}
public function loadModel($name) {
$path = 'models/'.$name.'_model.php';
if (file_exists($path)) {
require 'models/'.$name.'_model.php';
$modelName = $name . '_model';
// **here i make the object**
$this->model = new $modelName();
}
}
}
Here is the About model
class about_model{
function __construct() {
echo 'test';
}
public function test() {
$test = 'test one';
}
}
Here is the About Conroller
class About extends Conroller {
function __construct(){
parent::__construct();
$this->model->test();
$this->view->render('/about');
}
}
You will need to call loadModel in your About controller before you refer to the model:
class About extends Conroller {
function __construct(){
parent::__construct();
$this->loadModel('about');
$this->about->test();
}
}