I'm new to MVC and Codeigniter, but I have something working although not as I'd like and wondered if someone could help me?
I have 2 pages in my website (league, players) in a codeigniter sub-directory and currently my URL to get to them are 'http://www.mydomain.co.uk/codeigniter/index.php/golf' and 'http://www.mydomain.co.uk/codeigniter/index.php/players'
1) How to I remove the index.php from the URL? I've tried $config['index_page'] = ''; in config/config.php and set the .htaccess file, but no luck.
2) I only want to point my default controller in routes.php to golf controller and let the controller handle whatever page is being asked for.
3) Have I set this up correctly or if not then what is the proper way? One controller OK?
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ codeigniter/index.php/$1 [L]
config/routes.php
$route['default_controller'] = 'golf';
$route['players'] = 'golf/players'; <-Don't really want this entry!
config/autoload.php
$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');
controllers/golf.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Golf extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->model('league_model');
$data['league'] = $this->league_model->get_League();
$data['title'] = 'League Table';
$this->load->view('templates/header', $data);
$this->load->view('templates/menu');
$this->load->view('league', $data);
$this->load->view('templates/footer');
}
public function players() { //Runs because of entry in config/routes.php
$route['players'] = 'golf/players';
$data['title'] = 'Players';
$this->load->view('templates/header', $data);
$this->load->view('templates/menu');
$this->load->view('players', $data);
$this->load->view('templates/footer');
}
}
?>
models/league_model.php
<?php
class League_model extends CI_Model {
public function __construct() {
}
public function get_League() {
$this->db->from("player");
$this->db->order_by("name", "asc");
$query = $this->db->get();
return $query->result_array();
}
}
?>
views/league.php
<p><?php echo $title; ?></p>
<?php foreach ($league as $item): ?>
<p><?php echo $item['name']." : ".$item['handicap']." : ".$item['bbnetbirdie']." : ".$item['bb4p'] ?></p>
<?php endforeach ?>
views/players.php
<p>This is players</p>
You .htaccess should look like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase codeigniter/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
And in config $config['index_page'] = ''; this is perfect
A simple .htaccess file i use :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
and on the config.php file, removing any reference to index.php
$config['index_page'] = ''
Related
if(){
redirect('Login');
}
I have a condition above that redirect the page to log in depending on some values. I can see in url that I get redirect because when I enter
http://www.myapp.com it changes to http://www.myapp.com/Login
Now In my route I have:
$route['default_controller'] = 'welcome';
$route['Login'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
My Login controller looks like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Login_model');
}
public function index(){
$data['title'] = 'GYM';
$this->load->view('login',$data);
}
}
In my view I have
But why Im getting
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
www.myapp.com
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7
I cant figure out where I got configuration wrong
Update:
change config file:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';//'AUTO';
$config['url_suffix'] = '';
$config['language'] = 'english';
$config['charset'] = 'UTF-8';
$config['enable_hooks'] = FALSE;
$config['subclass_prefix'] = 'MY_';
$config['composer_autoload'] = FALSE;
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
and
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
accessing:
http://www.myapp.com/index.php/Login
Works but all the above does not. I not getting why
Try including below lines on the .htaccess at the root level of the codeigniter app folder :
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
change your value like following
$route['login'] = 'Login';
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
if not work, replace this code in htaccess
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
I am beginner in Codeigniter.
In .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
In config.php file I have made changes as
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['url_suffix'] = '';
$config['base_url'] = '';
In route.php file I have made changes as
$route['products/register'] = 'products/register';
$route['products'] = 'products';
$route['default_controller'] = 'products';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Products Controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Products extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array('session', 'form_validation', 'email'));
$this->load->database();
$this->load->model("product_model");
}
public function index()
{
die("aaaaa"); exit;
}
public function register()
{
die("DSds");
}
}
When I request url in browser http://localhost/codei/products/register its shows 404 Page Not Found
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Try the above code in .htaccess and also set base url in config file
$config['base_url'] = 'http://localhost/codei/';
Use this it will definitely work for you.
Don't do anyting else all are perfect.
Only replace .htaccess code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
note: Make sure rewrite_mode is on in apache
I am having 404 when I uploaded the codeigniter to server. I tried every known method and still unable to find the solution
My .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
config.php
$config['base_url'] = 'http://example.com';
$config['index_page'] = '';
$config['uri_protocol'] = 'QUERY_STRING';
routes.php
$route['default_controller'] = 'lloyd';
Controller lloyd.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Lloyd extends CI_Controller{
public function __construct() {
parent::__construct();
}
public function index()
{
$theme = $this->aauth->get_system_var("theme");
$this->load->view($theme . '/home');
}
}
First with your uri_protocol
Try This
$config['uri_protocol'] = 'REQUEST_URI';
Instead of
$config['uri_protocol'] = 'QUERY_STRING';
File names must have first letter upper case on CI3 versions same with classes.
Lloyd.php
Instead of
lloyd.php
With base url all ways good to use / at end also.
$config['base_url'] = 'http://example.com/';
Make sure you have the htaccess file on the main directory of your project.
After I remove index.php, I enable query string in codeigniter . But I have some trouble with redirect link. Detail , I have login form (login/index) and when login success redirect to "welcome/index" and save email in session .
But when login success only load view of "welcome/index" and wrong link , now link is :
"?login/index" And session don't save . Please help me .
Here's my code
Login.php (Controller)
if($this->input->post('email') != '' && $this->input->post('password') != ''){
if ($this->user->CheckLogin($this->input->post('email'),$this->input->post('password')) == true)
{
$this->load->library('session');
$this->session->set_flashdata('email', $this->input->post('email'));
redirect('welcome/index', 'refresh');
}
else
{
$data['error_message'] = '* Email or Password is incorrect';
$this->load->view('login',$data);
}
}else{
$this->load->view('login',$data);
}
Welcome.php (Controller)
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('user');
$this->load->helper('url');
$this->load->library('session');
$data['email'] = $this->session->flashdata('email');
$this->load->view('welcome_message',$data);
}
}
welcome_message.php (View)
<?php
// Cant print email
echo $email;
?>
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you enable Query Strings then change the redirect like this
redirect('c=welcome &m=index', 'refresh');
Additionally it may required to write index.php? or only ? before c
Or if change configuation in config.php then you need change the c and m
like
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
for more info http://www.codeigniter.com/user_guide/general/urls.html#enabling-query-strings
I have codeigniter installed in root/igniter folder.
under root/igniter, I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
Pages open just as I would expect them to without using 'index.php'.
So, my home page lies at http://example.com/igniter and my registration page at http://example.com/igniter/registration opens as well.
However, in my registration form, the submit button which is handled by a CI controller redirects to
http://example.com/igniter/index.php/registration
I have made sure that my config.php under applications/config has
$config['index_page'] = "";
This is what my Registration controller looks like:
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->model('Registration_Model');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|callback_username_check|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[12]|matches[passconf]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_email_check');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('registration');
}
else
{
//input data into database here
$username = strtolower($_POST['username']);
$password = md5($_POST['password']);
$email = strtolower($_POST['email']);
$data = array
(
'username' => $username,
'password' => $password,
'email' => $email
);
$this->Registration_Model->addUser($data);
$this->load->view('registrationsuccess');
}
}
As far as I can see, I am not explicitly asking the controller to redirect to index.php/registration.
Use this in your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|images|css|js|user_guide)
RewriteRule ^(.*)$ /index.php/$1 [L]
Note the RewriteRule line in my code. There is no ? mark as you used. In RewriteCond you may use as many folders as you like and the mentioned folders have access. CodeIgniter can not access the resource from any folder that is not listed in RewriteCond here.
You should also point your index method at the method you want to receive it. For example, if your class is "Registration" your index method should look like this:
public function index()
{
this->registration();
}
Then do everything in the method called "registration." This will help keep things clear for other developers who might work on this code in the future.