Question

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>
Was it helpful?

Solution

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

OTHER TIPS

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'] = ''
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top