user1765862
user1765862

Reputation: 14145

default routing in codeigniter

I'm exploring codeigniter. On app startup default controller is changed to load my controller.

Controller properly loads the view and that's fine, so I'm guessing routing works as expected, but when I use (manually type on address bar other method on same controller) same url pattern /controller/method I'm getting 404 error, either view exist.

Do have to change some default routing behavior or something else is problem?

Thanks

Upvotes: 1

Views: 6534

Answers (3)

Lloric Mayuga Garcia
Lloric Mayuga Garcia

Reputation: 156

follow this

root_folder/.htaccess

to remove index.php in url

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

set base URL

root_folder/application/config/config.php

| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/
$config['base_url'] = 'http://[::1]/my-project/';

removing index.php in url, even on request post in form

root_folder/application/config/config.php

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';

set default controller, mine is 'home'

root_folder/application/config/routes.php

| controller and method URI segments.
|
| Examples: my-controller/index -> my_controller/index
|       my-controller/my-method -> my_controller/my_method
*/
$route['default_controller'] = 'home';

after that, make sure that the all controller file name is capitalize. also a class name.

this is also important mostly when you need to upload in a live server.

root_folder/application/controllers/Home.php

<?php

/**
 * 
 * 
 * @author Lloric Garcia <[email protected]>
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends MY_Controller {

    public function index() {

    }    

}

then this will be you url

http://[::1]/my-project/home


that is my set up even in live server

all of this came from

https://www.codeigniter.com/userguide3/index.html

Upvotes: 0

Vinit Kadkol
Vinit Kadkol

Reputation: 1285

Well this may be the because of index.php file as mentioned above or else if you would like get rid of index.php Kindly include .htaccess file in your application.

 config/config.php - modifiy 
 $config['base_url'] = 'index.php'
 $config['base_url'] = '' // set it to blank

For .htaccess file refer the below code

 RewriteEngine on
 RewriteCond $1 !^(index\.php|images|robots\.txt)
 RewriteRule ^(.*)$ /index.php/$1 [L]

Upvotes: 0

BobRock
BobRock

Reputation: 3467

I dont know if you already removed index.php from your url pattern, assuming that's the case you should type inside browser address field index.php/controller/method. (if you manually type url as you describe)

If you on the other hand do not want to use index.php on every link you can consider to remove that, more info here.

Upvotes: 2

Related Questions