I have issues when I try something as basic as, let's say, displaying a login form to a user : I have a Users controller in my module Users that has a function login_form() supposed to display the form. I also have a Controller Admin in my CI folder application/controllers/Admin.php which has a login() function that can call the user module.
The problem :
If I browse directly users/login_form, it works and calls the login_form() function.
But if Admin/login is browsed and calls Users/login_form as a module, I can't access use modules::run() - I have to use modules::load()
I tried with only a simple execution where class constructors and methods just write their name & loaded path (see full code below)
TEST A params
// URI called : admin/login
// Admin extends MX | Users extends MX
// called from Admin::login()
$this->users = Modules::load('users')->login_form();
result
// class constructor - extends
Admin - MX_Controller
# Fx in Admin
Fx : login
// class constructor - extends
Users - MX_Controller
# Fx in users
Fx : login_form
So while Modules::load() allows me to access the users/login_form method from Admin, I can't get Modules::run() working ...
TEST B (not working) params
// URI called : admin/login
// Admin extends MX | Users extends MX
// called from Admin::login()
$this->users = Modules::run('users/login_form');
result
// same result as above for Admin
// class constructor - extends
Users - MX_Controller
# !! login_form() FROM USERS IS NOT EXECUTED !!
TEST A does execute the login_form() function, but although TEST B do load the constructor of the Users Class, it never reaches the login_form() function ...
I was wondering if it could be a routing problem, but as the constructor of the class Users is called in both run & load, I doubt this could be that ..
Full Code
Admin controller (application/controllers/Admin.php) :
class Admin extends MX_Controller
{
public function __construct()
{
parent::__construct();
echo get_class().' - '.get_parent_class()." [".dirname(__FILE__)."]<br>\n";
}
function login()
{
echo "Fx : ".__FUNCTION__."<br>\n";
// works fine
// loads the Users class constructor
// execute login_form()
Modules::load('users')->login_form();
// Does only load User constructor
// Modules::run('users/login_form');
}
}
Users Controller (application/modules/users/controllers/Users.php)
class Users extends MX_Controller {
public function __construct(){
parent::__construct();
echo get_class().' - '.get_parent_class()." [".dirname(__FILE__)."]<br>\n";
}
public function login_form()
{
echo "Fx : ".__FUNCTION__."<br>\n";
}
}
Related
The structure is as follows:
modules/school/config
modules/school/controllers
modules/school/controllers/form.php
modules/school/models
modules/school/views
modules/school/views/form.php
modules/univ/config
modules/univ/controllers
modules/univ/controllers/form.php
modules/univ/models
modules/univ/views
modules/univ/views/form.php
Now, I don't like the school form anymore, so I want to include university form inside the school view. How do I do that?
$this->load->view('../univ/form'); // does not work
I'm assuming you use HMVC by wiredesignz
in this case you've 2 possibilites
direct call
$this->load->view('univ/form');
or via modules::run
create in your univ/form a function where you load this view - e.g.
class Form extends MX_Controller
{
public funciton view()
{
$this->load->view('form');
}
}
and in your School Class you simply call
class School extends MX_Controller
{
public funciton view()
{
echo modules::run('univ/form/view');
}
}
Can we call a controller and parsing any values or data to the controller from a method?
let's say that i have this method,
function loader(){
//some operations to call another controller
}
and from that method i want to call a controller named welcome.php wich is located in /application/controller
i'v tried this but it doesn't work
function loader(){
$open = new Welcome();
}
it says that Class Welcome not found
Sorry for my bad english
At first You have to include the file
include('welcome.php');
Then, create the object.
function loader(){
$open = new welcome();
//if you want to call a method in an object
$open->MyWelcomeMethod();
}
Makesure that your loader controller was extended to welcome controller.
Suppose controller welcome,my_controller are two controller and loader function in B then
class Welcome extends CI_Controller {
function my_fun() {}
}
then you can call my_fun() when you are entended from my_controller like
class My_Controller extends Welcome {
$open = $this->my_fun();
}
I put the following code in every controller under public function index(). As of now I have 3 controllers and it will increase until my website is finish. I need the code below in all pages (i.e. views).
$type = $this->input->post('type');
$checkin = $this->input->post('sd');
$checkout = $this->input->post('ed');
My question is where can I put the code above in just one location so it will be available on all pages (i.e. views) and avoid putting it in every controller.
If you have for example a main view file, and you need that code on every page, then I suggest you put in the main view file (view/index.php)
I think, with #KadekM answer, you should call a function every time in every controller, because you sad, you want this code in every controllers every function.
You can create your own controller (for example MY_cotroller) extending CI_controller, put shared code there, and then your three controllers should extend MY_controller.
You can then call it wherever you need it (or even put it to constructor if you need it everywhere).
Here's the sample I promised (assuming you have default codeigniter settings)
in core folder create file named MY_Controller.php
class MY_Controller extends CI_Controller{
protected $type;
protected $checkin;
protected $checkout;
protected $bar;
public function __construct()
{
parent::__construct();
$this->i_am_called_all_the_time();
}
private function i_am_called_all_the_time() {
$this->type = $this->input->post('type');
$this->checkin = $this->input->post('sd');
$this->checkout = $this->input->post('ed');
}
protected function only_for_some_controllers() {
$this->bar = $this->input->post('bar');
}
protected function i_am_shared_function_between_controllers() {
echo "Dont worry, be happy!";
}
}
then create your controller in controllers folder
class HelloWorld extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function testMyStuff() {
// you can access parent's stuff (but only the one that was set), for example:
echo $this->type;
//echo $this->bar; // this will be empty, because we didn't set $this->bar
}
public function testSharedFunction() {
echo "some complex stuff";
$this->i_am_shared_function_between_controllers();
echo "some complex stuff";
}
}
then for example, another controller:
class HappyGuy extends MY_Controller {
public function __construct() {
parent::__construct();
$this->only_for_some_controllers(); // reads bar for every action
}
public function testMyStuff() {
// you can access parent's stuff here, for example:
echo $this->checkin;
echo $this->checkout;
echo $this->bar; // bar is also available here
}
public function anotherComplexFunction() {
echo "what is bar ?".$this->bar; // and here
echo "also shared stuff works here";
$this->i_am_shared_function_between_controllers();
}
}
Those are just examples, of course you won't echo stuff like this, but pass it to view etc., but I hope it's enough for illustration. Maybe someone comes with better design, but this is what I've used a few times.
id suggest , adding it to a library and then auto load the library so that each and every page on your website can access the same.
for autoloading reffer : autoload in codeigniter
I'm passing a user object from the controller to the view, then calling a method on that controller. I've done a print_r on the object in the view, so I know it's the right object with the right values. The current_user variable is an instance of the user class.
Here is the line in the layout that gives the error.
<?php echo $this->current_user->get_avatar_url(); ?>
Here is the method in the user class it's calling
public function get_avatar_url()
{
return !empty($this->avatar) ? $this->avatar : $this->fb_userid != '' ? "http://graph.facebook.com/".$this->fb_userid."/picture" : "/public/images/pukie.jpg";
}
This is the error I get
Fatal error: main() The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "User" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition in /home/breathel/public_html/application/views/layouts/layout.phtml on line 48
I'm including the full controller base where this in called in case it makes a difference
<?php
Zend_Loader::loadClass('Zend_Controller_Action');
Zend_Loader::loadClass('User');
class BaseController extends Zend_Controller_Action
{
protected $auth;
protected $current_user;
protected $db;
protected function initialize_values()
{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$this->current_user = $auth->getIdentity();
$this->view->current_user = $this->current_user;
}
$this->db = Zend_Registry::get('dbAdapter');
$this->view->controller_name = $this->_request->getControllerName();
$this->view->view_name = $this->_request->getActionName();
}
}
Zend Framework's authorisation module uses sessions to preserve identity across page load and is probably serialising the User model under the covers (especially if you're just assigning the result of a Zend_Auth_Adapter call).
Try including the User class before the first call to getIdentity() and see if that fixes it (even if you're confident you're not serialising it yourself).
I have two controllers a and b.
I would like to call a method of controller a from a method of controller b.
Could anyone help explain how I can achieve this?
This is not supported behavior of the MVC System. If you want to execute an action of another controller you just redirect the user to the page you want (i.e. the controller function that consumes the url).
If you want common functionality, you should build a library to be used in the two different controllers.
I can only assume you want to build up your site a bit modular. (I.e. re-use the output of one controller method in other controller methods.) There's some plugins / extensions for CI that help you build like that. However, the simplest way is to use a library to build up common "controls" (i.e. load the model, render the view into a string). Then you can return that string and pass it along to the other controller's view.
You can load into a string by adding true at the end of the view call:
$string_view = $this->load->view('someview', array('data'=>'stuff'), true);
test.php Controller File :
Class Test {
function demo() {
echo "Hello";
}
}
test1.php Controller File :
Class Test1 {
function demo2() {
require('test.php');
$test = new Test();
$test->demo();
}
}
Very simple way in codeigniter to call a method of one controller to other controller
1. Controller A
class A extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function custom_a()
{
}
}
2. Controller B
class B extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function custom_b()
{
require_once(APPPATH.'controllers/a.php'); //include controller
$aObj = new a(); //create object
$aObj->custom_a(); //call function
}
}
I agree that the way to do is to redirect to the new controller in usual cases.
I came across a use case where I needed to display the same page to 2 different kind of users (backend user previewing the page of a frontend user) so in my opinion what I needed was genuinely to call the frontend controller from the backend controller.
I solved the problem by making the frontend method static and wrapping it in another method.
Hope it helps!
//==========
// Frontend
//==========
function profile()
{
//Access check
//Get profile id
$id = get_user_id();
return self::_profile($id);
}
static function _profile($id)
{
$CI = &get_instance();
//Prepare page
//Load view
}
//==========
// Backend
//==========
function preview_profile($id)
{
$this->load->file('controllers/frontend.php', false);
Frontend::_profile($id);
}
I posted a somewhat similar question a while back, but regarding a model on CI.
Returning two separate query results within a model function
Although your question is not exactly the same, I believe the solution follows the same principle: if you're proposing to do what you mention in your question, there may be something wrong in the way you're coding and some refactoring could be in order.
The take home message is that what you're asking is not the way to go when working with MVC.
The best practice is to either use a Model to place reusable functions and call them in a controller that outputs the data through a view -- or even better use helpers or libraries (for functions that may be needed repeatedly).
You can do like
$result= file_get_contents(site_url('[ADDRESS TO CONTROLLER FUNCTION]'));
Replace [ADDRESS TO CONTROLLER FUNCTION] by the way we use in site_url();
You need to echo output in controller function instead of return.
You can use the redirect() function.
Like this
class ControllerA extends CI_Controller{
public function MethodA(){
redirect("ControllerB/MethodB");
}
}
Controller to be extended
require_once(PHYSICAL_BASE_URL . 'system/application/controllers/abc.php');
$report= new onlineAssessmentReport();
echo ($report->detailView());
You can use the redirect URL to controller:
Class Ctrlr1 extends CI_Controller{
public void my_fct1(){
redirect('Ctrlr2 /my_fct2', 'refresh');
}
}
Class Ctrlr2 extends CI_Controller{
public void my_fct2(){
$this->load->view('view1');
}
}
very simple
in first controllr call
$this->load->model('MyController');
$this->MyController->test();
place file MyController.php to /model patch
MyController.php should be contain
class MyController extends CI_Model {
function __construct() {
parent::__construct();
}
function test()
{
echo 'OK';
}
}