TheOrdinaryGeek
TheOrdinaryGeek

Reputation: 2323

Codeigniter 3 Routes and 404 Pages

I'm developing a web application and i'm slightly confused by routes and how they work.

My web application has an admin area and the URL structure is as follows;

example.com/admin/view/form/123

My Admin controller looks like this;

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

class Admin extends CI_Controller {

    public function index()
    {
        $data = array(
            'title'         =>  'Admin Page'
        );
        $this->load->view('admin/index', $data);
    }

    public function view() {
        $form_submission_id = $this->uri->segment(4);
        $records = $this->Admin_model->getDetails($form_submission_id);
        $data = array(
            'title'         =>  'Form Details',
            'records'       =>  $records
        );
        $this->load->view('admin/view/index', $data);
    }

}

I don't have any custom routes setup.

When I visit the following URL, I can see the page and corresponding data successfully;

example.com/admin/view/form/123

But, when I change the /form/ URL segment to something random like below I can still see the correct data;

example.com/admin/view/foo/123

Perhaps i'm misunderstanding the logic and should have my controllers / routes setup differently?

Upvotes: 2

Views: 51

Answers (2)

Mehdi Zamani
Mehdi Zamani

Reputation: 343

Rohit Mittal answer is good and also,
You can change the view fuction in admin controller like as:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller {

public function view($form = null,$form_submission_id = null) {
    if($form == "form" && $form_submission_id){
    $records = $this->Admin_model->getDetails($form_submission_id);
    $data = array(
        'title'         =>  'Form Details',
        'records'       =>  $records
    );
    $this->load->view('admin/view/index', $data);
 }
}

Upvotes: 1

Rohit Mittal
Rohit Mittal

Reputation: 2104

Codeigiter URL has a structure as domain/controllerName/actionName/param1/param2 and so on. In your code URL example.com/admin/view/form/123 admin is controller, view is action name and form and 123 is the parameters which you passed using get method. You can access these parameters like $this->uri->segment(3).

Thus in your code:

  1. It will not show any error as your function is not even using 3rd URI segment.
  2. It will not show 404 page as it found correct controller and action.
  3. To achieve domain related functionality, you need to either change for function code accordingly or need to use routes for this.

Hope it helps you to clarify this code.

Upvotes: 1

Related Questions