How to URL rewrite in CodeIgniter? - php

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

Related

Why 2 Controller Name appear in uri segment 1 at codeigniter?

when i code href="<?= base_url(loket/index_pos_bakum) ?>" then run it, it run normali. im directed to base_url/loket/index_pos_bakum. but the problem comes when i type code like before with deffrent value, base_url(admin/loket_admin). it doesnt gonna redirect to admin/loket_admin but its redirect to base_url/loket/admin/loket_admin. the first controller name always appear when im gonna try to redirect to any controller.
defined('BASEPATH') or exit('No direct script access allowed');
class Loket extends CI_Controller
{
public function index_pos_bakum()
{
$data['loket'] = $this->db->get_where('nomor_antrian', ['id_loket' => 0])->result_array();
$data['antrian'] = $this->db->query("SELECT MAX(nomor) AS nomor FROM nomor_antrian WHERE id_loket = 0 ")->row_array();
$this->template->load('template', 'operator/v_pos_yankum', $data);
}
}
And this is my second controller
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Admin_model', 'a_model');
}
public function loket_admin()
{
$this->template->load('template','admin/index_loket');
}

CodeIgniter routes issues to access the frontend and backend folder

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';

Making controllers in Codeigniter?

I have some controller in Codeigniter like this
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
// Load globals
$data->mail = new phpmailer();
$data->minified = new Minifier($vars_minified);
$this->load->vars($data);
}
public function index()
{
$this->home();
}
public function home()
{
$data['meta_title'] = seo::text($this->lang->line('home_title'), 70);
$data['body_render']='view_home';
$this->load->view("/layouts/view_layout", $data);
}
}
This is Home controller, that has __construct
Now i want to make new controller like page, but problem is with contruct , do i must repeat __construct in that controller too, or i just can put it somehow that all controllers will us that contsruct?
This is page controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
// Load globals
$data->mail = new phpmailer();
$data->minified = new Minifier($vars_minified);
$this->load->vars($data);
}
public function index()
{
$this->page();
}
public function page()
{
$data['meta_title'] = seo::text($this->lang->line('home_title'), 70);
$data['body_render']='view_page';
$this->load->view("/layouts/view_layout", $data);
}
}
You see, i have again __construct at top, is it possible to get rid of it, and make one unique __construct?
Yes it is possible please follow this guide.
When done, you can set your __construct for all Public_Controller[s], Admin_Controller[s] etc.
It is exactly what you want.
Note that after this step your variables that you send to views are going to change a bit: from $data['key'] , to $this->data['key']consider your data as "global" in scope of either Public_Controller or Admin_Controller etc.
If any troubles write a comment or read this core extending guide by ellislab or this SO thread where I mention this very same method.

Codeigniter - domain.de/this-is-a-test

How can i "split" a releated url with a - ?
So i`ve a URL like "domain.de/thisismytest"
But i want to have "domain.de/this-is-my-test" ?
Currently i`ve this in my routes.php
$route['this-is-a-test'] = "site/thisisatest";
In my really simple Controller i`ve this Code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_home");
$this->load->view("site_footer");
}
public function thisisatest() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_thisisatest");
$this->load->view("site_footer");
}
public function about() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_about");
$this->load->view("site_footer");
}
}
Your configuration seems to be correct. This routing config line:
$route['this-is-a-test'] = "site/thisisatest";
remaps to the Site class and the thisisatest method.
Are you sure that you do not have this routing in config:
$route['thisisatest'] = "site/thisisatest";
? In that case both would point to the same method.
Anyways I think that the problem is that you're referencing the thisisatest URLs somewhere in your views. Check if your not calling site_url('thisisatest') or base_url('thisisatest') or simply domain.de/thisisatest somewhere in the views and replace it with site_url('this-is-a-test').
In your index function you could grab the url segment and processes the string:
$segment = $this->uri->segment(1); // the url segment you need
$segments = explode( '-', $segment );
$yourFunction = implode ( $segments );
check if the function exists
if(method_exists($this,$yourFunction))
$this->$yourFunction();
and in your routes file point to your index:
$route['yourController/(:any)'] = 'yourController/index';

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