URL routing in PHP Codeigniter

假如想象 提交于 2020-07-08 20:42:46

问题


I am having entry in my route.php like - $route['admin/students'] = 'view_student'. Here view_student is controller name. Now when from "localhost/school/admin" page I call <a href="admin/students">Students</a>, than everything works fine; But when I change my route like - $route['/school/admin/students'] = 'view_student', and call it from "localhost/school/admin" page as <a href="/school/admin/students">Students</a>, than 404 page is shown. Whats wrong in here?


回答1:


Try this code it might help you :

Here dashboard is the name of controller

//this will route as localhost/appFolder/admin/index
  $route['admin'] = 'dashboard'; // for your index page

//this will route as localhost/appFolder/admin/method_name
 $route['admin/(:any)'] = 'dashboard/$1';

//this will route as localhost/appFolder/admin/method_name/param1
$route['admin/(:any)/(:any)'] = 'dashboard/$1/$2';

Link the route Like

// for your index page
<a href="<?php echo base_url('admin/index'); ?>"></a>

// for your other pages
<a href="<?php echo base_url('admin/method_name'); ?>"></a>

To link the other controller defined just like

 <a href="<?php echo base_url('otherControllerName/method_name'); ?>"></a>



回答2:


school is your ci root, so if you define $route['/school/admin/students'], it will seek school class with admin function, that never exist, instead of admin route.

you should read the documentations first before make any step, https://www.codeigniter.com/userguide3/general/routing.html



来源:https://stackoverflow.com/questions/38292243/url-routing-in-php-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!