I Am Groot I Am Groot - 4 months ago 15
PHP Question

CodeIgniter defaulting views

I've got this controller called Page

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Page extends CI_Controller {

/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index($page = 'home')
{
if ( ! file_exists(APPPATH.'views/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

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


but currently it only loads the home.php page by going to http://www.example.com/

I'm trying to make it so if I go to example.com/test it would load test.php

Here are my routes

$route['default_controller'] = 'page';
$route['(:any)'] = 'page/$1';


I am new to php frameworks, and I'm not sure how to achieve this. Any help would be much appreciated.

Answer

Codeigniter routing works in a way that it would point example.com/test to a the index function of a controller called Test.php in your controllers folder.

The following is the conventions for codeigniter url routing:

http://www.example.com/controller/function_in_that_controller/parameters/in/that/function

At http://www.example.com/controller/, the index function is called by default; since no function is defined in the url after the controller.