Codeigniter always redirecting to the same method - php

I have a system on the production server which I want to emulate in local to update it but the previous programmer have use a strange architecture. There is a codeigniter architecture and another one inside the first...
application
admin (which content controller and models... probably unused)
common (language and models...)
controllers
js (why here...)
kanri
cache, config, controllers, core, helpers, models, views, ...
user
cache, config, controllers, core, helpers, models, views, ...
system
etc...
I had a lot of fun to emulate this on my local machine because all path are absolute, yeepee.
So currently, it's on my localhost, correctly configurated but when I try to login, the page just reload. I found out the method login of the controller login (so kanri/login/login) is called but always routed to kanri/login/index.
On the index page, there is only a form which redirect to the kanri/login/login method, so even the simple redirection do not work.
The file kanri/route.php :
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
The main .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^kanri(/(.*))?$ kanri.php?/$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user(/(.*))?$ user.php?/$2 [L]
</IfModule>
MY_Controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
if($this->session->userdata('flag_admin_login')===FALSE || empty($this->session->userdata('flag_admin_login'))){
//print "***".$this->session->userdata('member_id')."!!!!!";
redirect('/', 'refresh');
}
}
}
My Login controller :
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
echo $this->router->fetch_class() . "<br/>"; // display the controller
echo $this->router->fetch_method() . "<br/>"; //display the method
}
public function index(){
//all code commented
}
public function login(){
//all code commented
}
}
I have a lot to work to do on it and I'm still stuck on the running...
Thanks for your time!

The solution was quite simple...
$config['uri_protocol'] = 'REQUEST_URI';
$config['enable_query_strings'] = FALSE;

Related

routing of static pages in codeigniter

I'm new to PHP and CodeIgniter, and I'm having trouble setting up the routing of static pages in my CodeIgniter app.
I have the ciblog/pages/views/about and it works.
I want to change it into ciblog/about.
I already have in the routes.php the $route['(:any)'] = 'pages/view/$1';
and it doesn't work it says in the browser:
page not found..
Please please help me I want to learn how to set up a static page....
Here is the code in the routes.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'welcome';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
?>
This is in my controller
<?php
class Pages extends CI_Controller{
public function views($page = 'home'){
if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header');
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer');
}
}
?>
here is in the .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
It will work, If your Controller is along the lines of...
This is the simplest, bare bones option.
class Pages extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('welcome_message');
}
public function view($page) {
$this->load->view($page);
}
}
Where the view method accepts a parameter.
Of course you would need to check that the view file exists etc and take the appropriate action.
Update:
Now that you have shown your Controller, I can see you are referring to view in your routes BUT you Controller method is called views ( with an added s).
They have to be the same.
So the quickest way to make them the same is to change
$route['(:any)'] = 'pages/view/$1';
to ( By adding an s to views so its the same as the controller method )
$route['(:any)'] = 'pages/views/$1';
OR you can simply change the method name from views to view.
Your choice!
Assuming the "about" page is to be served by the view method of the pages controller,
$route['about'] = 'pages/view/about';

Why are my MVC controllers showing 404?

I've got an MVC project I'm trying to set up on a new computer. I'm using MAMP and codeigniter. For some reason I get a 404 error when I try to go to any controller.
But the weird thing is that the default controller, defined in the routes file seems to work ok.
Here's an example controller:
class Test_controller extends CI_Controller {
public function index(){
echo 'hello';
}
}
When I go to localhost:8888/test_controller I get 404.
However, adding the following line to my routes.php...
$route['default_controller']="test_controller";
...and then navigating to localhost:8888 seems to load the controller ok.
You are missing the .htaccess, by default CI will need /index.php/controller to work, to remove the index.php you can use the HTaccess provided by Codeigniter or this one:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Place this outside the application folder with the name ".htaccess"
On Codeigniter CI3 which is case sensitive.
Make sure the file name has first letter upper case
Test_controller.php
And then controller class
<?php
class Test_controller extends CI_Controller {
public function index() {
echo 'hello';
}
}
How to use Controllers Link
Codeigniter Doc's Link
Also make sure your base_url is set
$config['base_url'] = 'http://localhost/project/';

CodeIgniter index function not triggered even though the controller has been called

The short: The code sample below does not echo "Here I am!" It will not echo from the _construct function. It will echo if I move the echo statement above the class so I know CI is reaching this controller.
The backstory:
I've inherited a project done in CodeIgniter 2.1.2. I'm supposed to replicate an application to another directory with a different subdomain and pointing the database config to a different database. I have the database updated in the config. I've updated the base bath. I've set environment to development so I can get error messages. There are no errors.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
if (!ini_get('date.timezone')) {
date_default_timezone_set('my-time-zone');
}
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('mod_login');
$this->load->helper('date');
}
function index() {
echo "Here I am!"; //Nothing echos
}
function logout{
//logout function here.
}
}
Any CodeIgniter fans here have an idea why?
Your controller should be like this.
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('mod_login');
$this->load->helper('date');
}
public function index()
{
echo "Here I am!";
}
}
and controller name should be loging.php
and in config/routes.php
$route['default_controller'] = "login";/set default conntoller
Extra Notes: (to remove index.php in URL)
in config/config.php
$config['base_url'] = '';
$config['index_page'] = '';
and .htaccess (place outside application folder)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Class name of controller must be uppercase. From documentation:
Note: Class names must start with an uppercase letter.
In other words, this is valid:
<?php
class Blog extends CI_Controller {
}
?>
This is not valid:
<?php
class blog extends CI_Controller {
}
?>
More: https://ellislab.com/codeigniter/user-guide/general/controllers.html#hello

Error: A default route has not been specified in the routing file in CI 3.0

I have successfully run the test webpage in my local machine and it works! But when I uploaded it to the production server (iPage), I got this error:
An Error Was Encountered
Unable to determine what should be displayed. A default route has not been specified in the routing file.
I have here the .htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|assets|img|bg|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Outside the application folder.
and in my routes.php:
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
and in my config.php:
$config['base_url'] = 'http://example.com/System';
$config['uri_protocol'] = 'REQUEST_URI';
$config['enable_query_strings'] = TRUE;
I have login.php inside controller folder with the ff codes:
defined('BASEPATH') OR exit('No direct script access allowed');
class login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login_model');
/* enable session */
$this->load->library('session');
}
public function index()
{
if ( ! file_exists(APPPATH.'/views/index.php'))
{
/* Whoops, we don't have a page for that! */
show_404();
}
$this->load->view('index');
$this->load->view('templates/footer');
}
Am I missing or did something wrong?
CodeIgniter 3 requires that your classes are named in a Ucfirst manner and the filenames must match the class names.
Therefore, you need to rename your 'login.php' to 'Login.php', as well as change the class declaration to class Login extends CI_Controller.
Your routes contain
$route['default_controller'] = 'login';
So in controller page name should be login.php
Inside login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller//check this
{
public function __construct()
{
parent::__construct();
}
Brief: check this class Login extends CI_Controller
And base_url should be
$config['base_url'] = '';

CodeIgniter does not load the functions from my controller

I started with CodeIgniter today, and I’m following the beginner tutorial from phpacademy.
Right now I got an odd problem, I got the following very simple controller:
<?php if ( ! defined('BASEPATH')) exit("No direct script access allowed");
class Site extends CI_Controller {
public function index()
{
}
public function home()
{
$this->load->view("view_home", $data);
}
function about()
{
$data['title'] = "About!";
$this->load->view("view_about", $data);
}
function test()
{
echo "test";
}
}
It always loads the index function fine. When I test it, it echoes whatever I want it to echo. But when I call the other functions nothing happen.
I didn’t set up my .htacces yet, but when I visit the following address:
localhost:8899/index.php/site/home
it doesn’t load the home function. same goes for the other function (“about” and “test”);
When I call one of the functions (“home”, “about” and “test”) from within the index() function it does call it how it should:
class Site extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->view("view_home", $data);
}
I’m not sure what is going wrong here. Hopefully, someone can guide me in the right direction!
My Routes:
<?php if ( ! defined('BASEPATH')) exit("No direct script access allowed");
$route['default_controller'] = "site";
$route['404_override'] = '';
If somebody came across this since no solutions above worked, ok you can try these steps where i got the thing worked (local with xampp):
Change the base_url in config.php into http://localhost/application-name/
Empty the index_page (the default value should be index.php)
Go to root folder of your application and check that index.php is there
Make .htaccess file in there and write into that these:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
i would make sure the controller class has a constructor to load the base codeigniter base classes.
function __construct()
{
parent::__construct();
}
in each controller class tells your class to use the code igniter system. if this is not present it won't know to how to load these functions.
Just to be clear, make sure it calls the other methods by url/site/about etc...
Also, have you got php errors displaying on you development server?

Categories