Reputation: 2323
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
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
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:
404
page as it found correct controller and action.Hope it helps you to clarify this code.
Upvotes: 1