CodeIgniter routes issues to access the frontend and backend folder - php

I am using CodeIgniter. I have frontend and backend folder inside controllers and views. I tried server steps even check almost all the solution but still I am not able to access it my default controller
routes.php
$route['default_controller'] = 'frontend/User_control';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
/*backend*********************************/
$config['backend'] = 'backend/Access_control';
1) My issue is When I am accessing the Url http://localhost/example_ci_row/
I am getting 404 Page not found
2) How to access the backend URL I tried http://localhost/icube_row/admin
but I am getting the error
frontend User_control
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_control extends CI_Controller {
public $current_date;
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('frontend/login');
}
}
?>
Backend Access control
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Access_control extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('backend/login');
}
}
?>
Edited
It's working when I use below steps. I added the Test.php file in the controller and change the routes then I am getting the login page.
Test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public $current_date;
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('frontend/login');
}
}
?>
routes.php
$route['default_controller'] = 'Test';

Hope this will help you :
The built in $route['default_controller'] will not work for sub-folders. you have to extend system router as per your requirements like this :
You need to create a MY_Router.php in application > core > MY_Router.php
<?php
class MY_Router extends CI_Router {
protected function _set_default_controller() {
if (empty($this->default_controller)) {
show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
}
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
// This is what I added, checks if the class is a directory
if( is_dir(APPPATH.'controllers/'.$class) ) {
// Set the class as the directory
$this->set_directory($class);
// $method is the class
$class = $method;
// Re check for slash if method has been set
if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
}
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
// This will trigger 404 later
return;
}
$this->set_class($class);
$this->set_method($method);
// Assign routed segments, index starting from 1
$this->uri->rsegments = array(
1 => $class,
2 => $method
);
log_message('debug', 'No URI present. Default controller set.');
}
}
This will allow you to use $route['default_controller'] = 'frontend/User_control'; as your default controller

Have you configured the Base URL ?
Application->config->config.php
$config['base_url'] = 'http://localhost/example_ci_row';

Related

Seperate site and admin in codeigniter

My Project Structure is
application
controllers/site
core/
views/site
I want when i entered http://mywebsite admin has to be loaded and when i entered http://mywebsite/site frontend has to be loaded
I went through some tutorials and i have done changes below
In config/routes.php
$route['default_controller'] = 'admin';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['site'] = 'site/home';
In core/My_Controller.php
defined('BASEPATH') or exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
class Admin_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
}
class Site_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
}
In controller/site/Home.php
defined('BASEPATH') or exit('No direct script access allowed');
class Home extends Site_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view("site/index.php");
}
}
But I am getting 404 when I opened http://mywebsite/site
Please help me
$route['site'] = 'site/home';
What that line does is that it defines a route that looks for a method called home inside the controller called Site viz. Site_Controller.
So you are getting the 404 error because your Site_Controller does not have the home method.
Try changing the Site_Controller like so...
class Site_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
# added this method
public function home()
{
$this->load->view("site/index.php");
}
}

How to extend a common controller by other controllers in Laravel 5.2?

I am new to Laravel. I have extended a common controller by other controllers in Codeigniter. Here is my CI common controller.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common extends CI_Controller {
protected $_data = array();
public function __construct() {
parent::__construct();
$this->load->helper(array('url','form','html'));
$this->load->library(array('session','authentication','upload','image_lib','pagination'));
$this->_data['totalUser'] = 10;
$this->_data['newUser'] = 2;
$this->_data['totalChallenge'] = 1;
$this->_data['totalReport'] = 1;
}
}
This is how I extended it in other CI controllers:-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once('common.php');
class Dashboard extends Common {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
}
public function index()
{
$data['globalData'] = $this->_data;
$entity = 'dashboard';
$action = 'view';
$data['action'] = $action;
$data['entity'] = $entity;
$this->authentication->is_loggedin($this->session->userdata('user_name'));
$this->load->view('admin/dashboard',$data);
}
}
Now, in view page, I can just print the values like <?php echo $globalData['totalUser'];?>
Why am I doing this?
Suppose, today I need to add 20 more data in all the controllers. Then I will have to change only in the common controller. Since $data['globalData'] = $this->_data; will make all 20 data available in every controller, it is easy for coder to write one LOC instead of 20 LOC.
How can I do the above work in Laravel 5.2?
In Laravel you have a base controller located in App\Controller.
You can use it exactly as are you using your CIController, because all the generated controllers (by Artisan) are extending that base project controller.
You can easily create the base controller which extents Laravel Controller in same directory ie(/controllers)
Following sample
DashboardController.php
class DashboardController extends \BaseController {
public function index()
{
$data['globalData'] = $this->_data;
$entity = 'dashboard';
......
}
BaseController.php (extends laravel Controller)
<?php
class BaseController extends Controller {
/**
*
* define your value
*/
protected $_data = "20";
}
please add if anything is missing

Codeigniter 3 show_404 function issue - MY_Exception doesn't load

I'm using Codeigniter 3 and I want to custom the show_404() function.
Here is my main controller:
class Welcome extends CI_Controller {
public function index() {
$this->load->view('welcome_message');
}
public function otherMethod() {
show_404();
}
}
Added on my routes.php
$route['404_override'] = 'error/show_404_custom';
Created the controller Error.php with the method show_404_custom
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Error extends CI_Controller {
public function show_404_custom() {
$this->load->view('404_read_view');
}
}
The 404_read_view.php view:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p>Custom 404 page.</p>
</body>
</html>
Then I've tested with some undefined URL like:
/index.php/welcome/method_A
And I successfully get my Custom 404 page.
However, if I run a method that uses the show_404() function like:
/index.php/welcome/otherMethod
I oddly get the standard Codeigniter 404 page.
I've searched a bit and found out that I should Extend the Exception core class and override the show_404() function like this:
https://stackoverflow.com/a/8425433/4301970
So, I created the MY_Exceptions.php class inside /application/core:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions {
public function __construct() {
parent::__construct();
}
public function show_404() {
$CI =& get_instance();
$CI->load->view('404_read_view');
echo $CI->output->get_output();
exit;
}
}
And when I run the URL:
/index.php/welcome/otherMethod
I get the error:
Fatal error: Class 'MY_Exceptions' not found in (...)\system\core\Common.php on line 196
I took look inside Common.php and noticed that the load class function looks inside the libraries directory instead of core directory?
function &load_class($class, $directory = 'libraries', $param = NULL)
How can I solve this problem?
use this, it's work in CI 3.1.4 (my latest project)
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::__construct();
}
function show_404($page = '', $log_error = TRUE)
{
$CI =& get_instance();
$CI->load->view('404_read_view');
echo $CI->output->get_output();
exit;
}
}
When you need to use CI3 404_override cannot be in subfolder
Change
$route['404_override'] = 'error/show_404_custom';
// thinks its in subfolder
To
Location: application > controllers > Error.php
$route['404_override'] = 'error';
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Error extends CI_Controller {
public function index() { // change to index
$this->load->view('404_read_view');
}
}

How to URL rewrite in CodeIgniter?

My routes configuration only contains following route:
$route['default_controller'] = "frontend/home";
I would like to rewrite the default route
http://192.168.1.4/sncraft/frontend/home/about
to an url like this
http://192.168.1.4/sncraft/about
The content of the controller is:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function __Construct(){
parent::__Construct();
$this->load->library('image_lib');
$this->load->model('frontend/front_model');
}
public function index(){
$config = array();
$config["base_url"] = base_url()."frontend/home";
$this->viewForm();
}
public function about(){
$this->load->view('frontend/header');
$this->load->view('frontend/about');
$this->load->view('frontend/footer');
}
}
?>
The key of the $route array is the actual url which leads to the function of the controller (without the base url).
Your route would look like this:
$route['about'] = "frontend/home/about";
I'm assume that your function inside the home-controller is called about().
You find more information here: URI Routing

How to call one controller function in another controller in codeigniter

I have one controller named home.php in which a function named podetails is there. I want to call this function in another controller user.php.
Is it possible to do so? I have read about HMVC in CI, but I want to know is it possible to do without using hmvc?
To extend controller please either follow this tutorial or see some code below.
differences between private/public/protected
make a file in folder /application/core/ named MY_Controller.php
Within that file have some code like
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $data = Array(); //protected variables goes here its declaration
function __construct() {
parent::__construct();
$this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not
$this->load->model('some_model'); //this can be also done in autoload...
//load helpers and everything here like form_helper etc
}
protected function protectedOne() {
}
public function publicOne() {
}
private function _privateOne() {
}
protected function render($view_file) {
$this->load->view('header_view');
if ($this->_is_admin()) $this->load->view('admin_menu_view');
$this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
$this->load->view('footer_view');
}
private function _isAdmin() {
return TRUE;
}
}
and now in any of yours existing controllers just edit 1st or 2nd line where
class <controller_name> extends MY_Controller {
and you are done
also note that all your variables that are meant to be used in view are in this variable (array) $this->data
example of some controller that is extended by MY_Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class About extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['today'] = date('Y-m-d'); //in view it will be $today;
$this->render('page/about_us'); //calling common function declared in MY_Controller
}
}
write the podetails() as a function within a helper file.
then load that helper in both of the controllers.
in the controller you just call podetails()
Suppose:
--controller 1--
function podetails()
{
podetails(); // will call function in helper ;
}
--controller 2--
function podetails()
{
podetails(); // will call function in helper ;
}

Categories