0

I'm new in Codeigniter I'm not sure how to use Codeigniter Routing. I've created the Contact.php in the controller folder and contact.php in the views folder.

In routes.php I have put $route['Contact'] = 'controller/contact'; but when I enter the url http://mytest.dev/contact/ it shows 404 Page Not Found. The page you requested was not found.

I want when I enter "http://mytest.dev/contact" it will show the contact page

Thanks in advance.

ArmKh
  • 435
  • 9
  • 31
Valorm
  • 9
  • 2
  • You only need routes when you want to change CodeIgniter's standard handling of URLs. See [CodeIgniters URL](https://www.codeigniter.com/user_guide/general/urls.html) documentation for details. – DFriend Apr 06 '18 at 15:46
  • 1
    A good Stack Overflow question should include code. In this case sharing your controller would be good. That said, I think @ArmKh has given you the answer. – DFriend Apr 06 '18 at 15:48
  • This is [https://github.com/abkr/codeigniter-contact-form/blob/master/controllers/contact.php] contact on github @DFriend thank you. – Valorm Apr 06 '18 at 16:06

2 Answers2

1

Controller

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

    class Contact extends CI_Controller {
        function __construct() {
            parent::__construct();
        }

        function index() {
            $this->load->view('contact')
        }
    }

In CI there is an index.php in the URL ( by default ). So, you can access your page with this url http://mytest.dev/index.php/contact

For removing it from URL and have it like you want you need to add .htaccess file in your project directory

Check this answer for it

Also, you don't need to change your routes.php every time after creating a new page. Leave it like this

$route['default_controller'] = 'welcome'; // or contact
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
ArmKh
  • 435
  • 9
  • 31
0

I see you are using $route['Contact'] = 'controller/contact';

Tell us your controller's class name and which function/method did you use from the above you are referencing to contact() and that controller name doesn't make sense. You route will normally be in lowercase too.

If you named your class Contact (which seems like it) then you need to put a .htaccess file in the folder where your index.php or base_url resides (or root directory) and then remove the value in application/config.php as $config['index_page'] = ''; so that you can access it from http://mytest.dev/contact

To make it clearer. The format should be $route['AAA'] = 'BBB/CCC';

AAA is the url path of your choice BBB is the name of your controller's class CCC is the function/method of the page you want to show

If you didn't add the htaccess, you must put /index.php/ before your preferred path.

Rangka Kacang
  • 327
  • 1
  • 5
  • 12