omkara
omkara

Reputation: 982

How to remove controller from url in codeigniter?

controller:Test.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller 
{
    function __construct() 
    {
        parent :: __construct();
        $this->load->helper(array('form', 'url', 'captcha', 'email', 'html'));
    }
    public function study_abroad()
    {
        $data['student_id'] = $this->session->userdata('student_id');
        $this->load->view('study-abroad',$data);
    }
}

view:

<a href="<?php echo base_url(); ?>study-abroad">Study Abroad</a>

route.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;

$route['(:any)'] = "colleges/index/$1";
$route['college/(:any)'] = "test/college/$1";
$route['study-abroad'] = "test/study_abroad";

In this code, I have created a controller having name Test.php where I have load a view file having name study-abroad. Now, I want to remove controller name i.e. Test from my URL. I have defined the path of study abroad in my route file and delete controller name from view file but it redirects me to the wrong page. If I put controller name before view file it will redirect me the actual page. What is the problem behind this?

Upvotes: 0

Views: 417

Answers (2)

always-a-learner
always-a-learner

Reputation: 3794

Try this Complete Route file code:

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

$route['college/(:any)'] = "test/college/$1";
$route['study-abroad'] = "test/study_abroad";
$route['(:any)'] = "colleges/index/$1";

$route['default_controller'] = 'test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

As indicated, $route['(:any)'] will match any URL, so place your other custom routes before the other route route

Calling a route in view:

<a href="<?= base_url('study-abroad') ?>">Study Abroad</a>

Hope It Helps.

Upvotes: 1

user8442578
user8442578

Reputation:

You can set your a route for each url in Code Igniter. In your config/routes.php file, just set each page like this:

$route['study-abroad'] = "test/study_abroad";

Hope it helps you

Upvotes: 0

Related Questions