Accessing controller's function - php

I am using Codeigniter framework to develop a website. I am currently working on home.php view under the view folder. I need to use UserInfo() function which is inside one of the controllers. Any suggestion how to access that function?
class Welcome extends CI_Controller {
public function UserInfo(){
$this->load->model('model_user');
$data['title'] = 'Users';
$data['users'] = $this->model_user->getUser();
$this->load->view('template/users', $data);
}
}

You cant call controller method inside another controller. Its No Way to do it.
You have two way to resolve this issue
If you want to access the function which place inside the
controller, add that into an model. So by loading model you can call
it.
use redirect('welcome/UserInfo') if you just need to call the function

As You want to call controller function in other controller.In codeigniter App folder core folder exists you make a custom core controller and all other controllers extend with your custom controller
In your Custom Core controller
class CustomCore extends CI_Controller
{
/* ---YOUR FUNCTION IN CUSTOMCORE---- */
public function mycorefunc()
{
//Do something
}
}
and your all other controllers extend with custom core
class YourController extends Customcore
{
function controllerfunction()
{
$this->mycorefunc();// Call corefunction
}
}

Related

Yii2: Setting layout for a specific controller

I want to change layout for all views inside my controller. So I want to set like this:
class SiteController extends Controller {
public function __construct(){
$this->layout = 'admin';
}
.....
But I am getting the following error:
Call to a member function getUniqueId() on null
By default yii2 uses the main layout as the layout for project and it's controllers, but if you want to use another layout for a controller or change the layout name and use that layout for a controller to have to define a layout property in your controller class and give the name of your layout as the string value of that property.
This will change the layout of that controller to the demanded layout with your chosen name.
Here is the code in your case:
class SiteController extends Controller
{
public $layout = '[Your Layout Name]';
.
.
.
}
P.S: Constructor is the method which runs whenever you create an instance of your class and using it in this case is not logical.
I used init() and worked as expected. I changed my code to:
class SiteController extends Controller {
public function init() {
$this->layout = 'admin';
}
....
Your sitecontroller extends to parent Controller. So you can create constructor in parent controller but not in sitecontroller....
try using
public function init(){
}
in site controller

Autoload function and get variables in codeIgniter

I have this question: how to load function in codeIgniter in every page and get data or variables from it to show in view?
I have many controllers and each one has many function. Each function load master view as a template and load its own view. Master view has dynamic data I push it from database.
So I need in master view to show some data from database automatically. How to do it?
You can use this. change it according to your
function index()
{
$data['list'] = $this->some_model->show_data();
$data1['content'] = $this->load->view('some_view',$data,true);
$this->load->view('template',$data1);
}
content is in your main template where u will pass your data to show in template
I am not sure what exactly you ask but if you want a function to be loaded automatically to any controller
then create a Mycontroller.php in application/core
class Mycontroller extends CI_Controller
{
function __construct()
{
parent::__construct();
// here goes your function
}
}
and then extend every controller you have to Mycontroller instead of CI_Controller
if you want to use your function inside views. use CI Helpers
Codeigniter Helpers
create your helper function
function get_data() {
// and use CI instance
$CI = & get_instance();
// now you can call your models, libraries. etc. in the helper
$data = $CI->your_model->your_model_funcion();
return $data;
}
and you can call your helper function in controllers, views, models. etc...
get_data();

Is there a way to have multiple controllers for a symfony 1.4 module?

I have a situation where I have a base controller (a base actions.php file) in Symfony 1.4. I want to create another controller, for the same module, that extends that base controller.
I need to extend that base controller because I want to customize the behavior of certain visitors, that are identified based on an ID in the URL.
Any hints?
Another controller class for the same module, I think it's impossible in symfony.
I guess the easiest solution for you is to create another method in the same class, and then invoque it from the base one.
By Example: actions.class.php:
public function executeBaseAction(sfWebRequest $request) {
.. if($user....) then return $this->executeCustomAction($request);
}
public function executeCustomAction(sfWebRequest $request) {
// $this->setTemplate('anotherTemplate?');
}
Actually you can add another controller class for the same module.
You could include several files in your action directory in this way:
In action1Action.class.php
class action1Action extends sfAction
{
public function execute($request) {
//Your code here
}
}
This will use template action1Success.php
In action2Action.class.php
class action2Action extends sfAction
{
public function execute($request) {
//Your code here
}
}
This will use template action2Success.php

Using an abstract Base Controller in CodeIgniter

I have a set of controllers that should only be accessible if you are an admin (as opposed to a regular user).
Thus, in the constructor for each of the controllers, I would do:
public function __construct() {
parent::__construct();
if (! is_admin()) {
show_404();
}
}
Instead of adding this code to the constructor of every Admin Controller, is there a better way to do this?
I was thinking I could create a Base controller called Admin_Controller that would look like this:
public class Admin_Controller extends CI_Controller {
public function __construct() {
//the above code goes here
}
}
And then all my other controllers can extend this class, instead of the CI_Controller class. The only problem with this is, I need to include this file at the top of my other controllers, or CodeIgniter cannot find Admin_Controller.
Is there a better way to do this?
place this in your application/core folder: MY_Controller.php (note the correct use of capitals)
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
if (! is_admin())
{
show_404();
}
}
Then in all your normal controllers that you want users to be logged in
class Whatever extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
See userguide here on extending core classes
I think that you could use Hooks
I've done the same thing, but I require a much more precise control so I have to check on every controller. Not all controllers are forbidden for single users.

Calling member function of other controller in zend framework?

Is it possible to call the member function of another controller in zend framework, if yes then how?
<?php
class FirstController extends Zend_Controller_Action {
public function indexAction() {
// general action
}
public function memberFunction() {
// a resuable function
}
}
Here's another controller
<?php
class SecondController extends Zend_Controller_Action {
public indexAction() {
// here i need to call memberFunction() of FirstController
}
}
Please explain how i can access memberFunction() from second controller.
Solution
Better idea is to define a AppController and make all usual controllers to extend AppController which further extends Zend_Controller_Action.
class AppController extends Zend_Controller_Action {
public function memberFunction() {
// a resuable function
}
}
class FirstController extends AppController {
public function indexAction() {
// call function from any child class
$this->memberFunction();
}
}
Now memberFunction can be invoked from controllers extending AppController as a rule of simple inheritance.
Controllers aren't designed to be used in that way. If you want to execute an action of the other controller after your current controller, use the _forward() method:
// Invokes SecondController::otherActionAction() after the current action has been finished.
$this->_forward('other-action', 'second');
Note that this only works for action methods (“memberAction”), not arbitrary member functions!
If SecondController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class, so that both controllers can access the shared functionality without having to depend on each other.
You should consider factoring out the code into either an action helper or to your model so that it can be called from both controllers that need it.
Regards,
Rob...
I would suggest you to follow DRY and move those functions to common library place. For example create in library folder
My/Util/
and file
CommonFunctions.php
then call your class
My_Util_CommonFunctions
and define your methods
Now you can call them from any place in the code using your new namespace which you have to register.
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace(array('My_'));
in any controller you can call your custom methods by using:
My_Util_CustomFunctions::yourCustomMethod($params);

Categories