5

私は CodeIgniter の初心者で、最初のプロジェクトを仕上げています。ただし、ホスティング サイトに配置する前に、CodeIgniter が構成フォルダーで提供する routes.php ファイルを使用して URL をクリーンアップしたいと考えています。

私のサイトは、次の URL を使用して読み込まれます。

http://fakehost:8888/TheWorksPlumbing/index.php/theWorksPlumbingController/index/home
http://fakehost:8888/TheWorksPlumbing/index.php/theWorksPlumbingController/index/about
http://fakehost:8888/TheWorksPlumbing/index.php/theWorksPlumbingController/index/services

また、次のデフォルトのコントローラー URL を使用してホームページをロードします。http://fakehost:8888/TheWorksPlumbing/

ただし、サイトの各ページに URL を設定したいのですが、うまくいきません。たとえば、次のようにします。

http://fakehost:8888/TheWorksPlumbing/home
http://fakehost:8888/TheWorksPlumbing/about
http://fakehost:8888/TheWorksPlumbing/services

theWorksPlumbingController コントローラ ファイルのコードは次のとおりです。

class TheWorksPlumbingController extends CI_Controller {

    public function index($page = 'home'){

        if ( !file_exists('application/views/'.$page.'.php') ) {
            show_404();
        }

        $this->load->view('templates/header');
        $this->load->view($page);
        $this->load->view('templates/footer');
    }
}

これは、動作していない routes.php ファイルのコードです。

$route['default_controller'] = "theWorksPlumbingController";
$route['404_override'] = '';
$route['(:any)'] = "index.php/theWorksPlumbingController/index";

サイトが /home または /about または /services のみをロードするようにするには、何を追加または変更する必要がありますか?

4

1 に答える 1

7
$route['home'] = 'theWorksPlumbingController/index/home';
$route['about'] = 'theWorksPlumbingController/index/about';
$route['services'] = 'theWorksPlumbingController/index/services'; 

できるかもしれませんが、そのような設定で試したことはありません。通常、CI では、次のようにページごとにメソッドを作成します。

class TheWorksPlumbingController extends CI_Controller {

    public function home(){

        $this->load->view('templates/header');
        $this->load->view('home');
        $this->load->view('templates/footer');
    }

    public function about(){

        $this->load->view('templates/header');
        $this->load->view('about');
        $this->load->view('templates/footer');
    }

    public function services(){

        $this->load->view('templates/header');
        $this->load->view('services');
        $this->load->view('templates/footer');
    }
}

経由で到達可能

http://www.example.com/index.php/theWorksPlumbingController/home
http://www.example.com/index.php/theWorksPlumbingController/about
http://www.example.com/index.php/theWorksPlumbingController/services

経由でルーティング可能

$route['home'] = 'theWorksPlumbingController/home';
$route['about'] = 'theWorksPlumbingController/about';
$route['services'] = 'theWorksPlumbingController/services';

https://www.codeigniter.com/user_guide/general/routing.html

于 2013-03-21T18:47:23.920 に答える