Can anyone tell me: Do I have to declare all of my controllers in Routes.php in Codeigniter 4?
I can't seem to get a controller to work unless I add it directly to the "Routes.php"
I have created my controllers properly and the Home controller is working after install and setup.
If I add the controller My_page.php :
<?php
namespace App\Controllers;
class My_page extends BaseController{
public function index(){
echo "Controller 'My_page' -> function index() ";
}
}
?>
I get a : "404 - File Not Found Sorry! Cannot seem to find the page you were looking for."
If I now add the controller to the rout - i.e.:
$routes->post('my_page', 'My_page::index');
Then my controller works properly and I get the "Controller 'My_page' -> function index() " when I visit www.mydomain.com/my_page
I have also tested: www.mydomain.com/index.php/my_page
and this makes no difference.
I am using the .htaccess that comes with the download. I have updated the base URL to www.mydomain.com/
The documentation is confusing to me - https://www.codeigniter.com/user_guide/incoming/routing.html#setting-routing-rules ;
it sounds like they are saying have to declare all classes with routes?
Why are my controllers not working without declaring them specifically in Routes.php?
And am I misunderstanding ' setAutoRoute(true)
' it also does not seem to work - I expect that I can turn this on and simply create my controllers pretty much like in CI3?
1条答案
按热度按时间fd3cxomn1#
如果你不启用自动路由,你肯定需要添加你允许的所有路由,其他任何路由都将失败,并出现错误404。正如@parttimeturtle提到的- autoroute,它从4.2开始默认被禁用。
简而言之-是的,您需要添加所有控制器、它们的功能和相应的http方法。(这也包括CLI路由)
你可以使用$route-〉add(),它允许所有的http方法,但是用它们的方法显式地设置它们更安全。