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
Related
I need to send the same result to almost every view page, so I need to bind the variables and return with every controller.
My sample code
public function index()
{
$drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
$locations = Location::get();
return view('visitor.index', compact('drcategory','locations'));
}
public function contact()
{
$drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
$locations = Location::get();
return view('visitor.contact', compact('drcategory','locations'));
}
But as you see, I need to write same code over and over again. How can I write it once and include it any function whenever I need?
I thought about using a constructor, but I cannot figure out how I can implement this.
You are able to achieve this by using the View::share() function within the AppServicerProvider:
App\Providers\AppServiceProvider.php:
public function __construct()
{
use View::Share('variableName', $variableValue );
}
Then, within your controller, you call your view as normal:
public function myTestAction()
{
return view('view.name.here');
}
Now you can call your variable within the view:
<p>{{ variableName }}</p>
You can read more in the docs.
There are a few ways to implement this.
You can go with a service, a provider or, like you said, within the constructor.
I am guessing you will share this between more parts of your code, not just this controller and for such, I would do a service with static calls if the code is that short and focused.
If you are absolutely sure it is only a special case for this controller then you can do:
class YourController
{
protected $drcategory;
public function __construct()
{
$this->drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
}
// Your other functions here
}
In the end, I would still put your query under a Service or Provider and pass that to the controller instead of having it directly there. Maybe something extra to explore? :)
For this, you can use View Composer Binding feature of laravel
add this is in boot function of AppServiceProvider
View::composer('*', function ($view) {
$view->with('drcategory', DoctorCategory::orderBy('speciality', 'asc')->get());
$view->with('locations', Location::get());
}); //please import class...
when you visit on every page you can access drcategory and location object every time
and no need to send drcategory and location form every controller to view.
Edit your controller method
public function index()
{
return view('visitor.index');
}
#Sunil mentioned way View Composer Binding is the best way to achieve this.
I feel I repeat myself in Laravel (4) controllers. For example, I need some $variables for every view. I get them from cache or database. Because geting and processing them take some lines of codes (between 5-100 lines), I repeat them in every controller function. This is especially a problem when updating functions (They increase complexity).
How can I avoid this without any negative effect?
Note: $variables are mostly not global variables.
There are many ways to go about this, but it sounds like the variables are more specific to your View's than your controllers.
You can easily share variables across your views in the boot method of your AppServiceProvider.
view()->share('variables', function(){
//you can share whatever you want here and it will be availble in all views.
});
You can create a static helper class (all static functions)
e.g.
class VarHelper {
public static function getVars() {
return [];
}
}
You can create your own basecontroller you extend in every other controller
e.g.
class MyController extends Controller {
public function getVars() {
return [];
}
}
class BlaController extends MyController {
public function doBla() {
$vars = $this->getVars();
}
}
creating the function inside the controller and call it in the other functions
use a Trait
And probably more solutions
Create a trait with a method to set all of the necessary variables.
use View;
trait SharedControllerVariables {
public function loadUsersSubscription() {
View::make('user_subscription_variable_1', [...data...]);
View::make('user_subscription_variable_2', [...data...]);
}
}
Then call it in your controller constructor:
class MyController extends Controller {
use SharedControllerVariables;
public function __construct() {
$this->loadUsersSubscription();
}
}
Title pretty much sums it up. I'd like to write a script to import some data from a third party. Is there any way I can get either an instance of CodeIgniter's Active Record class, or of a particular model inside of an a script that can be run from the command line?
Say you have your CodeIgnited application inside ignited/, you have your command line script running in cli/. Here is your folder layout:
ignited/
application/
system/
index.php
cli/
mycli.php
Now you want cli/mycli.php to use some model from ignited/
First, you should do something with your ignited app:
Modify your default controller (which is set in ignited/application/config/routes.php). For example ignited/application/controllers/welcome.php:
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
add these code:
if ($this->input->is_cli_request())
{
return;
}
so it would look like:
class Welcome extends CI_Controller {
public function index()
{
if ($this->input->is_cli_request())
{
return;
}
$this->load->view('welcome_message');
}
}
For testing, I created a test model ignited/application/models/do_nothing_model.php:
class Do_nothing_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function do_nothing()
{
return "Do nothing";
}
}
OK! Now try this in cli/mycli.php:
require_once('../ignited/index.php');
$ci =& get_instance();
$ci->load->model('do_nothing_model');
echo $ci->do_nothing_model->do_nothing();
Drawback: unable to run the default action of your default controller in command line
UPDATE 2012-06-08: from CodeIgniter Wiki
Not sure if this is what you want, but if you just need to run CI from CLI, here is the userguide reference:
http://codeigniter.com/user_guide/general/cli.html
To clarify, you don't need it web facing, just setup your app and run the commands you need (controller method input):
$ php index.php tools message "John Smith"
that example would run the primary index.php file for CI, then the tools controller and method message, "John Smith" is your input to message (could be a file reference that is local on the machine for your file import).
i have this testing code which am working with ..
i have a module called ms and and another one called test
the test controller code is :
<?php
class Test extends MX_Controller {
public function __construct()
{
parent::__construct();
$this->template->title($this->config->item('site_name','app'));
}
public function index()
{
$this->template->build('index');
}
}
and the code inside ms is :
<?php
//ms module
class Msrofi extends MX_Controller {
public function __construct()
{
parent::__construct();
$this->template->title($this->config->item('site_name','app'));
}
public function index()
{
$t = Modules::run('test/test/index');
var_dump($t);
$this->template->build('index_message');
}
}
the problem is that the build function inside test is trying to find the index view file inside the ms views folder not the test views folder ..
i checked the $this->_module and it gave me the ms module name ..
any one know how to fix that ??
Since the test module is being called in the context of the ms one, $this->template->build() is looking for a view file in the ms module. The same way you can load models and libraries cross-module, you would have to do this for your view path as well:
class Test extends MX_Controller {
public function index()
{
// This path works only from the "test" module
// $this->template->build('index');
// This path works from any module
$this->template->build('test/index');
}
}
It's a little annoying maybe to have to explicitly call the module path in the module itself, but cross-module dependency defeat some of the goals of modularity in the first place.
A quick aside: Modules::run() output not returned, but directly echoed, so you can't assign it to a variable or print_r/var_dump it without using an output buffer:
ob_start();
Modules::run('test/test/index');
$t = ob_get_clean();
var_dump($t);
You can try to change the module.php the run method
The following example is I have to use the fix solution:
Open the third_party/MX/Modules.php
Near 75 lines to find
$buffer = ob_get_clean();
Increase in its following:
if($output === NULL && $buffer === ''){
$output = CI::$APP->output->get_output();
}
At this time, it should be able to work properly...
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';
}
}