Using the default public function index() in creating custom controller in CodeIgniter - php

I am new to CodeIgniter framework and just thought to ask this when I am studying the framework. I just want to ask, is including the function public function index(){....} before adding other functions after that? Or you can not necessary add the function and just load a view? For example, if I don't create the public function index(){...} and just add a function, let's say public function test_index(){..} and inside it is load the view..

I see the whole point now. I can add a function other than the public function index(){..} and just call or load the view inside it. For example:
public function test_index(){
$this->load->view('home_view);
}
and just view in the browser together with the class and the function..

You can add other functions as you wish with or without index

Related

Accessing Controllers methods for sidebar in Laravel

I am making a project with Laravel 5.6 and at the moment I am making a sidebar with links to access the functionality of specified controller. f.e. if I am in posts blade, it will show PostsController methods for the sidebar.
The problem is that every controller has different amount of methods, and I wouldn't want to make a mess with 10 different static layouts for sidebars.
Is there a way to access controller methods thru functionality that returns all methods of the controller to a view?
Or am I thinking this wrong.. If someone knows a better solution for this i'm all ears. :)
I know I can install packages for functionality but I want to know before that is there any simple solution.
EDIT1:
get_class_methods($this) returns following value:
Returned Methods of a Controller
I can add a validator that checks if "index" or "create" is present. Guess my problem was solved, thank you all who answered.
EDIT2:
The code that dumps the returned methods.
public function index()
{
$events = Event::all();
dd($controller = get_class_methods($this));
return view('events.index', compact(['events', 'controller']));
}
You could use the ´get_cass_methods´ function to grab all the methods on the controller class
function index() {
$methods = get_class_methods($this);
return view('posts', compact('methods'));
}
if you want to filter out methods from the parent class
function index() {
$methods = array_diff(get_class_methods($this),get_class_methods(get_parent_class()));
return view('posts', compact('methods'));
}

Global Query Data

There's a part of a code (query) that will be required in all controllers, they will be passed into views for display.
Can I know is there anyway to declare them in just a single file so that I can reference them directly from my view? Without declaring them in each controller's _construct.
I'm using codeigniter3, here's a sample code:
MainController.php
public function index(){
$data['userCampaign'] = $this->Usermodel->getCampaign();
}
Create default controller in your project which extends CI_Controller and your all controller extends new controller and in __construct(); function of your new controller you can add this code.
Declare this function as protected in parent controller class.
No. I don't know there is method like that.
You want call the function in __construct() or you have to declare function in controller and call it back. $this->check_session()

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();

Opencart call function from module controller

I've tried asking this on Opencart forums (thread link), but still can't quite get it, though someone tried to explain this to me. I hope someone here can help.
I've written some extensions before where I have a custom function in the controller called from view , for example:
if I edit admin/controller/sale/customer.php and after index() function add
public function foo(){
//code here
}
I can access it by using
index.php?route=sale/customer/foo
Now I have a module in catalog, could I access a function in it's controller from view, in the below example "foo"?
my_module.php:
class ControllerModuleMyModule extends Controller {
protected function index($setting) {
...
}
public function foo(){
...
}
Basically, I want to make an AJAX call to it from whatever page/route the module is on.
Many thanks in advance.
This can be done in the same way you would for any module. For instance, if you'd added foo() to /catalog/controller/module/cart.php you would use
index.php?route=module/cart/foo
There's nothing special about the module controllers compared with any other accessible module

CodeIgniter adding page doesn't work

I have taken over a project written in CodeIgniter, which I have never used before. I have added a new file to views/pages called features.php, and have read on the internet that to make it accessible, I need to create a function in the controller file that will render the page.
I have tried the following:
public function features()
{
$this->render('template', 'pages/features');
}
However, when I try to open features.php, it gives me 404. How can I fix that?
Update 1 - Class
Here is the controller's class code:
class Pages extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->model('setting_model', 'setting');
$this->load->model('order_model', 'order');
$this->load->model('page_model', 'page');
$this->load->library('form_validation');
$this->load->helper(array('inflector', 'string'));
}
public function index()
{
$settings = $this->setting->get_settings();
$data['document_price'] = $settings->document_price;
$this->render('template', 'pages/index', $data);
}
//This works fine
public function about_us()
{
$this->render('template', 'pages/about_us');
}
//Here is the problem, although it follows the same pattern as about_us()
public function features()
{
$this->render('template', 'pages/features');
}
}
As you are using $this->render I guess you are using the template library. I think you should be using:
public function features()
{
$this->template->set_template('template');
$this->template->write_view('pages/features');
$this->template->render();
}
The php files contained in /views are not directly accessible by typing in some URL. CodeIgniter is an MVC framework. That means that your URLs are mapped to your controllers and the controllers call the views.
What is the name of the class that this function is encapsulated in? Please post the entire class and not just the features() function and we can help you out. If you're working locally, the default mapping to call controllers is: http://localhost/appname/controller/function/param1/param2/etc.
The $this->render() function is not vanilla CodeIgniter syntax, you either inherited a project that is using a templating library, or, there is a sibling render() function inside the controller class.
Check your config/routes.php file as well and consider posting it.
If you want to diagnose the issue, try pinpointing by removing the call to $this->render() and instead using CodeIgniter's native $this->load->view('pages/features') function. If this works, we can be sure it's the library or render() call.

Categories