Multi-level Controller - codeigniter

i have problem with viewing page within sub-subfolder. when i access the url it says page not found.
The url i used to access is localhost/amc/program/admin/user
Here is the folder structure i have:
controller
-program
--admin
user.php
core
-program
--admin
admin_controller.php
models
-program
--admin
user_model.php
views
-program
--admin
user_list.php
And here's the controller code
<?php
class User extends Admin_Controller
{
public $data = array(
'halaman' => 'user',
'main_view' => 'program/admin/user_list',
'title' => 'Data User',
);
public function __construct()
{
parent::__construct();
$this->load->model('program/admin/User_model', 'user_model');
}
public function index()
{
$user = $this->user_model->get_all_user_data();
if ($user) {
$this->data['userData'] = $user;
} else {
$this->data['userData'] = 'Tidak ada data user.';
}
$this->load->view($this->layout, $this->data);
}
}
thank's for your attention guys

I just found out that CI doesnt support multi-level sub-folder controller, but there's work around that enable us to do that.
I found this code that enable multi-level sub-folder controller:
multi-level controller
credit: https://github.com/ollierattue/codeigniter-multi-level-controller-extension/blob/master/core/MY_Router.php

Related

How to invoke controller methods in construct method in Laravel?

I'm trying to invoke multiple methods in __construct function of laravel controller so that all page partials can get their data before loading the whole page. Here is some code demonstration.
web.php
Route::get('/','HomeController#index');
HomeController.php
class HomeController extends controller
{
public function __construct()
{
$this->middleware("auth");
$this->featuredNews();
}
public function index()
{
return view('pages.home');
}
public function featuredNews()
{
$news = News::select('id', 'heading', 'body', 'category', 'image', 'created_at', 'featured')->where('featured', 1)->first();
return view('pages.home_partials.featured_news')->with('news', $news);
}
}
home.blade.php
#include("pages.home_partials.featured_news");
Here I'm expecting the the home.blade.php data along with the featured_news.blade.php partial's data. But this code throwing an error
ErrorException
Undefined variable: news (View: D:\portal\resources\views\pages\home_partials\featured_news.blade.php)
How can I add multiple partials data along with the blade data in Laravel ?
Laravel version: 7.30
You need to set the controller property for this instead of set the view into the methods so please find the below codes which help:
HomeController.php
class HomeController extends controller
{
private $news = [];
public function __construct()
{
$this->middleware("auth");
$this->featuredNews();
}
public function index()
{
return view('pages.home')->with('news', $this->news);
}
public function featuredNews()
{
$this->news = News::select('id', 'heading', 'body', 'category', 'image', 'created_at', 'featured')->where('featured', 1)->first();
}
}

How to calculate page views in Codeigniter and count by IP address?

Please explain steps how to calculate page views in codeigniter.
Is there any instruction on how to do this.
by using following method:
$this->input->ip_address();
How to Write $this->input->ip_address(); in Below Controller:
Controller Page
class About extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('array');
$this->load->model('about_model');
}
public function index() {
$data = array(
'page_title' => 'About',
'page_name' => 'site/aboutus/about',
'about' => $this->about_model->list_all(),
);
$this->load->view('site/template', $data);
}
}
?>
Model Page:
class About_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function list_all() {
return $this->db->get('about')->result_array();
}
}
$this->input->ip_address() to take the user's ip address in the controller
In the database you save the ip, the time the user first visited the site and a counter
Get the counter: $this->db->select_sum("counter")->get("views_table"); and process the result.

Codeigniter tidying up my controllers

I've followed Codeigniter's configuration straight out of the manual - and just wondered if there was a simpler or more efficient way of coding my controllers...
eg.
class Home extends CI_Controller {
public function index()
{
$this->load->helper('segment1');
$this->load->model('segment1/Leftsidebar_model');
$data['articles'] = $this->Leftsidebar_model->articles();
$this->load->model('segment1/Default_model');
$data['head'] = $this->Default_model->segment1();
$data['segment1'] = $this->Default_model->segment1();
$data['segment2'] = $this->Default_model->segment2();
$this->load->model('Rightsidebar_model');
$data['coming_up'] = $this->Rightsidebar_model->coming_up();
$data['featured_pages'] = $this->Rightsidebar_model->featured_pages();
$data['recommended_link'] = $this->Rightsidebar_model->recommended_link();
$data['testimonials'] = $this->Rightsidebar_model->testimonials();
$this->load->view('head_view', $data);
$this->load->view('header_view', $data);
$this->load->view('segment1/__leftSidebar_view', $data);
$this->load->view('segment1/__mainContent/default_view', $data);
$this->load->view('segment1/__mainContent/segment2_view', $data);
$this->load->view('__rightSidebar_view', $data);
$this->load->view('footer_view', $data);
}
}
helpers and models can be auto loaded, this can be specified in the confiq file. This will save you from having to manually load them.
As for the rest:
You can subclass CI_Controller. Eg:
My Controller extends CI controller and this would then contain a 2 methods
1) to load page head and header
2) to load page bottom
you could then subclass your controller from My_Controller and call those methods
load_page_top();
//insert whatever you have to load
load_page_bottom();
other than that the rest is up to you
Eg:
class Home extends MY_Controller
{
index()
{
$data = get_data();
load_page_top();
//insert your views here specific to the controller
load_page_bottom();
}
get_data()
{
//gather all your needed data here and return it as an array
return data;
}
}
I love this structure, clean and neat.
class Home extends CI_Controller {
public function index()
{
// Load libraries, helpers, models
$this->load->helper('segment1');
$this->load->model('segment1/Leftsidebar_model');
$this->load->model('segment1/Default_model');
$this->load->model('Rightsidebar_model');
// Data for views
$data = array(
'articles' => $this->Leftsidebar_model->articles(),
'head' => $this->Default_model->segment1(),
'segment1' => $this->Default_model->segment1(),
'segment2' => $this->Default_model->segment2(),
'coming_up' => $this->Rightsidebar_model->coming_up(),
'featured_pages' => $this->Rightsidebar_model->featured_pages(),
'recommended_link' => $this->Rightsidebar_model->recommended_link(),
'testimonials' => $this->Rightsidebar_model->testimonials()
);
// Load views
$this->load->view('head_view', $data);
$this->load->view('header_view', $data);
$this->load->view('segment1/__leftSidebar_view', $data);
$this->load->view('segment1/__mainContent/default_view', $data);
$this->load->view('segment1/__mainContent/segment2_view', $data);
$this->load->view('__rightSidebar_view', $data);
$this->load->view('footer_view', $data);
}
}
Of course, the view loading could be managed in other way, having a view loading all common sections.
I just write little additional library for rendering similar page blocks.
Something like this:
class Display_Lib{
private $_CI;
private $_template_data;
public function __construct()
{
$this->_CI =& get_instance();
}
public function set($key, $value)
{
$this->_template_data[$key] = $value;
}
public function get($key)
{
return $this->_template_data[$key];
}
public function get_template_data()
{
return $this->_template_data;
}
public function display_page($view, $data = array())
{
$this->set('content', $this->_CI->load->view($view, $data, TRUE));
$this->_CI->load->view('templates/main_template', $this->get_template_data());
}
}
Set this library in auto load:
$autoload['libraries'] = array('session', 'database', 'display_lib');
And call it in controller:
class Main extends CI_Controller{
public function index()
{
$some_data = array();
$this->display_lib->display_page('views/main_view', $some_data);
}
}

Autoload controller in CodeIgniter

I have question to you.
I try add to my page calendar and some events in this calendar. I know how I can load calendar in page, but I didn`t now how I can load this calendar on every page automatically.
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar extends CI_Controller {
function index()
{
$data = array(
3 => 'Polska - Anglia',
);
$this->load->library('calendar');
$vars['calendar'] = $this->calendar->generate('', '', $data);
$this->load->view('main/calendar', $vars);
}
}
and In view I call:
<?php echo $calendar;?>
I use CodeIgniter 2.1.3
Instead of creating controller for calendar, create a library class and then add it to autoload configuration file
class MyCalendar{
public function get()
{
$CI =& get_instance();
$data = array(
3 => 'Polska - Anglia',
);
$CI->load->library('calendar');
return $CI->calendar->generate('', '', $data);
}
}
Add this library to autoload file and then you can call it anywhere you want by using this statement.
$data['calendar'] = $this->MyCalendar->get();
You can autoload your library by changing the application/config/autoload.php file.
Find :
$autoload['libraries'] = array();
Replace by :
$autoload['libraries'] = array('calendar');
To load the same calendar on all your pages, I suggest to build a parent controller in the application/core folder.
abstract class BaseController extends CI_Controller
{
protected $data;
public function __construct()
{
parent::__construct();
$this->data = array();
$calendarData = array(
3 => 'Polska - Anglia'
);
$this->data['calendar'] = $this->calendar->generate('', '', $calendarData);
}
}
You can then extend that BaseController class on all your controllers.
class Calendar extends BaseController {
function index()
{
$this->load->view('main/calendar', $this->data);
}
}
Be sure to always use $this->data to build on the protected member of your class.
Lastly, your BaseController will not be autoloaded, you probably don't want to include it everywhere. I suggest you to add the following method at the end of your autoload or config file.
/**
* Extra autoload function. Loads core classes automatically.
* #param type $class The class to load.
*/
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'core/' . $class . EXT))
{
include $file;
}
}
}
This will allow you to autoload any classes you have in your application/core folder. This solution might seems complex, but once it's setup. You can add functionality to your BaseController that is applicable for all pages, for example, header and footer stuff.
Hope this helps!

Codeigniter index page

I want to create a page, which I will be able to browse like this: /page/5.
I have created a page.php controller and I can view this page, here is its content:
class Page extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('template/header');
$id = $this->uri->segment(2);
$this->load->view('template/middle', array('id' => $id));
$this->load->view('template/footer');
}
}
The problem is that I cant reach the page number from the uri.
To be clear:
This is reachable: index.php/page.
But at this url, I'm getting the 404: index.php/page/5
I know I can make another controller function to view the page like this /index.php/page/id/5, but I want it be be accessable via the index function.
Is that possible?
CodeIgniter doesn't know whether you're looking for 5() or index(5)
To fix this, Try setting up a custom route in config/routes.php:
$route['page/(:num)'] = 'page/index/$1';
Also, In your index function:
public function index($id)
{
$this->load->view('template/header');
$this->load->view('template/middle', array('id' => $id));
$this->load->view('template/footer');
}

Resources