php - Custom routing and controller function -
i have routes.php as
$route['home'] = 'pages/home'; $route['login'] = 'pages/login'; $route['default_controller'] = 'pages/home';
and controller pages.php as
class pages extends ci_controller { public function home() { $this->load->view('templates/header'); $this->load->view('pages/home'); $this->load->view('templates/one'); $this->load->view('templates/two'); $this->load->view('templates/footer'); } public function login() { //if ( ! file_exists('application/views/templates/'.$page.'.php')) { // echo "no file"; // } // $data['title'] = ucfirst($page); $this->load->view('templates/header'); $this->load->view('templates/login'); $this->load->view('templates/footer'); } }
pre: have started codeigniter , got basic tutorial , after reading many stackoverflow answers call domain/login
routed function login
in pages class
(controler) per the routing rule $route['login'] = 'pages/login';
the problem: simple code showing 404 error. not getting why so, files present in templates folder. normal call domain works fine if call domain/home
, again 404 error. kindly me doing wrong.
so bit new codeigniter apologize @ being slow on this. problem facing haven't put in index.php. url has domain/index.php/login. if don't want add index.php every call must following:
add .htaccess file in application folder , have this:
<ifmodule mod_rewrite.c> # activate url rewriting rewriteengine on # folders mentioned here accessible , not rewritten rewritecond $1 !^(resources|system|application|ext) # not rewrite php files in document root, robots.txt or maintenance page rewritecond $1 !^([^\..]+\.php|robots\.txt) # rewrite else rewriterule ^(.*)$ index.php/$1 [l] </ifmodule> <ifmodule !mod_rewrite.c> # if don't have mod_rewrite installed, 404's # can sent index.php, , works normal. errordocument 404 index.php </ifmodule>
turn on mode_rewrite (how enable mod_rewrite apache 2.2 or https://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache)
restart server
this forward domain/login requests front controller (index.php). line rewritecond $1 !^(resources|system|application|ext) allow make folders not rewritten. if have folder under application named "resources" instead of getting forwarded domain/index.php/resources go domain/resources.
in actuality without .htaccess file process this:
- check domain/front_controller/route pattern in uri , see if route exists , forward appropriately
by doing domain/login not following pattern , 404 delivered. adding front_controller (index.php) uri makes follow route pattern , gets forwarded route config.
some people think front controller in uri "ugly" add in mod_rewrite adds in front_controller uri every time directory accessed. way can stick domain/controller/action. considered more secure directories can directly accessed ones stated in rewrite.
Comments
Post a Comment