Default routing in codeigniter

I am learning codeigniter. When you start the application to start the controller, the controller changes by default.

The controller loads the view correctly and this is fine, so I assume that the routing works as expected, but when I use (manually type the address bar of another method on the same controller) the same url / controller / method template, I get a 404 error, there is either representation.

Do I need to change any default routing behavior or something else a problem?

thanks

0
php codeigniter
source share
3 answers

I do not know if you removed index.php from your url template, suggesting that the case you should enter in the browser address field is 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 for each link, you can consider it, more information here .

+2
source share

Well, it could be because of the index.php file, as mentioned above, or if you want to get rid of index.php. Please include the .htaccess file in your application.

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

For the .htaccess file, refer to the code below

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

follow this

root_folder / .htaccess

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  and you can easily do that on your own. | */ $config['base_url'] = 'http://[::1]/my-project/'; 

removing index.php in url, even on post request 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 the 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'; 

then make sure the controller file name is fully uppercase. also the name of the class.

This is also important when you need to load the server.

root_folder / application / controllers / home.php

 <?php /** * * * @author Lloric Garcia < emorickfighter@gmail.com > */ defined('BASEPATH') OR exit('No direct script access allowed'); class Home extends MY_Controller { public function index() { } } 

then it will be your url

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


which is configured even on a real server

it all came from

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

0
source share

All Articles