Remove Segment From URL Codeigniter - codeigniter

Is it possible to remove a segment from URL but still redirecting it to location. I use a MY_Controller for all the controllers.
Example: localhost/project/maintenance
Would become localhost/project
Router.php
$route['default_controller'] = "catalog/common/home/index"; // Always Default Controller
$route['404_override'] = '';
$route['maintenance'] = "catalog/common/maintenance"; // Maintenance Controller
I tried this below know luck
if($this->config->item('system_maintenance') == FALSE) {
$route['default_controller'] = "catalog/common/home/index";
} else {
$route['default_controller'] = "catalog/common/maintenance/index";
}

I Think I got it working now in routes works fine now so far.
if($this->config->item('system_maintenance') == TRUE) {
$route['default_controller'] = "catalog/common/maintenance/index";
} elseif ($this->config->item('system_maintenance') == FALSE) {
$route['default_controller'] = "catalog/common/home/index";
}

Related

How to fix session being auto generated when page is reloaded/refresh using Codeigniter and Database as session driver?

I have my little project running perfectly on my local computer. However, when I run it into my laptop, an entry is automatically loaded in my ci_sessions table each time the page is being reloaded or refresh. I am using the database as my session driver.
Based on the screenshot: row 4 says that my login session store successfully. However, the 2 extra rows (5, 6) that are being added cause this code to fail:
public function isLoggedIn()
{
if($this->session->userdata('logged_in') === true) {
redirect('home', 'refresh');
}
}
public function isNotLoggedIn()
{
if($this->session->userdata('logged_in') !== true) {
redirect('login', 'refresh');
}
}
here's my config.php
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
Here's my Page Controller
<?php
class Pages extends MY_Controller
{
public function view($page = 'login')
{
if (!file_exists(APPPATH.'views/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
if($page == 'login') {
$this->isLoggedIn();
$this->load->view($page, $data);
}
else{
$this->isNotLoggedIn();
$this->load->view($page, $data);
}
}
}
MY_Controller Class
<?php
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function isLoggedIn()
{
if($this->session->userdata('logged_in') === true) {
redirect('home', 'refresh');
}
}
public function isNotLoggedIn()
{
if($this->session->userdata('logged_in') !== true) {
redirect('login', 'refresh');
}
}
}
So far I have already tried adding the code below to my autoload.php but no luck.
$autoload['libraries'] = array('database', 'session');
Note: again this works in another unit with a similar setup.
after trying some work around, Codeigniter 3 is not yet compatible with php 7. I have to downgrade my php version to 5.6 to make it work. thanks folks for helping.
I suffered a lot of troubles with the original CI session library (included what you mention). Finally I arrived to this replacement that use native PHP session. It works!
Believe me, in the middle of a project, I did not stop to wonder why. Just works.
Here it is: CodeIgniter Native Session
BUT, due that it is an old library you MUST made some hacks. You can check those simple hacks in the library's forum
Just drop this file in codeigniter's library directory.

Codeigniter default_controlller and pagination

I get 404 page when i go to any page
Routes.php
$route['default_controller'] = "home";
$route['default_controller/page/(:any)'] = 'home/$1';
Controller (home.php)
public function index($ppage=1)
{
$this->load->library('pagination');
$config['use_page_numbers'] = TRUE;
$config['uri_segment'] = 2;
$config['num_links'] = 5;
$config['base_url'] = 'http://www.test.com/page/';
$config['first_url'] = 'http://www.test.com/page/1';
$this->pagination->initialize($config);
}
When i go to the URL http://www.test.com/page/1 or http://www.test.com/page/2 i get 404 no found. Where did i go wrong with this line?
$route['default_controller/page/(:any)'] = 'home/$1';
Default controller isn't a variable:
$route['home/page/(:any)'] = 'home/$1';
or if that doesn't work:
$route['page/(:any)'] = 'home/$1';
Kindof a weird approach imo.

codeigniter redirecting to a url

I have a website with a url like so (which works fine)
http://www.page.com/en/controller
But there always have to be a language and a controller in the url otherwise the page doesn't load or there is no language (no text).
Is it possible that when I enter a url like this
http://www.page.com
I get redirected to
http://www.page.com/en/controller
And the controller would be hidden? Only this would be left (my links require first segment to load a page with a particular language)
http://www.page.com/en
p.s tried routing and redirect, but with no luck
Sorry for the late response, I was away.
my routes
$route['default_controller'] = 'arena/display';
$route['(:any)/renginiai'] = "renginiai/getevents";
$route['(:any)/arena'] = "arena/display";
$route['(:any)/paslaugos'] = "paslaugos/displayServices";
$route['(:any)/kontaktai'] = "kontaktai/displayContacts";
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
My_Controller which is located in core folder. I load my language libraries from here.
/**
*
*/
class MY_Controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$languages = array("lt", "en");
if(in_array($this->uri->segment(1), $languages)){
$this->lang->load($this->uri->segment(1), $this->uri->segment(1));
}
}
}
And this is my front page controller
<?php
class Arena extends MY_Controller{
public function display($year = NULL, $month = NULL){
/*$this->load->model('Mycal_model');*/
$this->load->model('Image_model');
$data['images'] = $this->Image_model->get_image_data();
$this->load->view('includes/head');
$this->load->view('includes/nav', $data);
$this->load->view('includes/header', $data);
//$this->load->view('includes/calendar', $data);
$this->load->view('includes/section');
$this->load->view('includes/footer');
}
}
open application/config/routes.php file and change
$route['default_controller'] = "en/controller";

CodeIgniter server based 404

I've installed CodeIgniter on my localhost xampp server in the directory:
localhost/CI/
When I visit that directory directly I get to see the homepage but when I try to vist any other page I get a server based 404 page. I don't get the see the CI 404.
I already tried playing around with the uri_protocol but I can't get it to work. Any clue?
routes.php
$route['page/create'] = 'page/create';
$route['(:any)'] = 'page/view/$1';
$route['default_controller'] = 'page/view/hello-world';
$route['404_override'] = '';
Page controller
class Page extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('page_model');
}
public function view($slug)
{
$data['page'] = $this->page_model->get_page($slug);
if (empty($data['page']))
{
show_404();
}
$data['title'] = $data['page']['title'];
$this->load->view('templates/header', $data);
$this->load->view('page/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a new page';
$this->form_validation->set_rules('title', 'Title', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('page/create');
$this->load->view('templates/footer');
}
else
{
$this->page_model->set_page();
$this->load->view('page/success');
}
}
}
The "Hello world!" shows nicely, but I can't get the create page to work. The view is located in views/page/create.php
xampp !!!! is the problem the mode rewite is not working fine wid it .. install apache as a standalone , add php and mysql and you can work fine any way it will cause other erros
$route['default_controller'] = 'page/view/hello-world';
change that to
$route['default_controller'] = 'page';
then move it to the top so you have
$route['default_controller'] = 'page';
$route['404_override'] = '';
$route['page/create'] = 'page/create';
$route['(:any)'] = 'pages/view/$1';
the order of things is inportant inside the routes.php
and inside page.php controler add a function index()

Codeigniter - configure enable_query_strings and form_open

I want to be able to user query strings in this fashion. Domain.com/controller/function?param=5&otherparam=10
In my config file I have
$config['base_url'] = 'http://localhost:8888/test-sites/domain.com/public_html';
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO';
$config['enable_query_strings'] = TRUE;
The problem that I am getting is that form_open is automatically adding a question mark (?) to my url.
So if I say:
echo form_open('account/login');
it spits out: http://localhost:8888/test-sites/domain.com/public_html/?account/login
Notice the question mark it added right before "account".
How can I fix this?
Any help would be greatly appreciated!
The source of your problem is in the Core Config.php file where the CI_Config class resides. The method site_url() is used by the form helper when you are trying to use form_open function.
Solution would be to override this class with your own. If you are using CI < 2.0 then create your extended class in application/libraries/MY_Config.php, otherwise if CI >= 2.0 then your extended class goes to application/core/MY_Config.php.
Then you need to redefine the method site_url().
class MY_Config extends CI_Config
{
function __construct()
{
parent::CI_Config();
}
public function site_url($uri='')
{
//Copy the method from the parent class here:
if ($uri == '')
{
if ($this->item('base_url') == '')
{
return $this->item('index_page');
}
else
{
return $this->slash_item('base_url').$this->item('index_page');
}
}
if ($this->item('enable_query_strings') == FALSE)
{
//This is when query strings are disabled
}
else
{
if (is_array($uri))
{
$i = 0;
$str = '';
foreach ($uri as $key => $val)
{
$prefix = ($i == 0) ? '' : '&';
$str .= $prefix.$key.'='.$val;
$i++;
}
$uri = $str;
}
if ($this->item('base_url') == '')
{
//You need to remove the "?" from here if your $config['base_url']==''
//return $this->item('index_page').'?'.$uri;
return $this->item('index_page').$uri;
}
else
{
//Or remove it here if your $config['base_url'] != ''
//return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
return $this->slash_item('base_url').$this->item('index_page).$uri;
}
}
}
}
I hope this helps and I think you are using CI 2.0 that wasn't officially released, this was removed in the official CI 2.0 version
Simpler might be to just set follwing in your config.php
$config['enable_query_strings'] = FALSE;
Was the solution in my case.
If you want to use query string in your url structure, then you should manually type your url structure in the following order:
<domain.com>?c={controller}&m={function}&param1={val}&param2={val}
in the action of the resepective controller you should get the parameter as $_GET['param1']
your code now should look like this
form_open(c=account&m=login&param1=val)
Please let me know if it doesnt work for you.

Resources