Codeigniter第一次尝试

时间:2013-07-03 12:33:01

标签: codeigniter codeigniter-routing

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

我的网站(联盟,玩家)在codeigniter子目录中有2个页面,目前我要访问的网址是“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]

配置/ routes.php文件

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

配置/ autoload.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>

2 个答案:

答案 0 :(得分: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'] = '';中,这是完美的

答案 1 :(得分: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'] = ''