0

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?

7
  • Why you want to remove controller from the url? Commented Sep 18, 2017 at 7:56
  • I am give this controller name in many pages through which my url is long thats why @PankajMakwana Commented Sep 18, 2017 at 8:02
  • can any body help me plsss Commented Sep 18, 2017 at 8:02
  • Your code looks ok what is the problem? Commented Sep 18, 2017 at 8:05
  • checkout this documentation codeigniter.com/userguide3/general/routing.html this may help you Commented Sep 18, 2017 at 8:06

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@omkara remove test as default controller and try it work then we will see what to do with that
I have edit in my route file please look at once if I comment $route['(:any)'] = "colleges/index/$1"; $route['college/(:any)'] = "test/college/$1"; these two route then study-abroad work perfectly what is the reason @ankit suthar
@omkara Try the updated answer and just paste the route code and try.
Thanks buddy for helping me. its woking :)
just one thing that avoids using plane (any) in route. always give any name before it
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.