0

我是 MVC 和 Codeigniter 的新手,但我有一些工作虽然不是我想要的,但我想知道是否有人可以帮助我?

我的网站(联赛、球员)在 codeigniter 子目录中有 2 个页面,目前我访问它们的 URL 是“ http://www.mydomain.co.uk/codeigniter/index.php/golf ”和“ http://www.mydomain.co.uk/codeigniter/index.php/players '

1) 如何从 URL 中删除 index.php?我试过 $config['index_page'] = ''; 在 config/config.php 并设置 .htaccess 文件,但没有运气。

2)我只想将我在 routes.php 中的默认控制器指向高尔夫控制器,并让控制器处理所要求的任何页面。

3)我是否正确设置了这个,或者如果没有,那么正确的方法是什么?一个控制器好吗?

.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ codeigniter/index.php/$1 [L]

配置/路由.php

$route['default_controller'] = 'golf';
$route['players'] = 'golf/players'; <-Don't really want this entry!

配置/自动加载.php

$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');

控制器/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');
    }

  }

?>

模型/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();
    }

  }

?>

意见/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 ?>

意见/players.php

<p>This is players</p>
4

2 回答 2

1

.htaccess应该看起来像这样

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

在配置中$config['index_page'] = '';这是完美的

于 2013-07-03T12:47:47.660 回答
0

我使用的一个简单的 .htaccess 文件:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

并在 config.php 文件中,删除对 index.php 的任何引用

$config['index_page'] = ''
于 2013-07-03T13:32:48.103 回答