简体   繁体   English

CodeIgniter是否始终将控制器与URI段关联?

[英]Does CodeIgniter always associate a controller to a segment of a URI?

This is my first attempt to work/learn CodeIgniter. 这是我第一次尝试/学习CodeIgniter。 However, I'm struggling in understanding the "C". 但是,我正在努力理解“ C”。

1) Does CodeIgniter always associate a controller to a segment of a URI? 1)CodeIgniter是否始终将控制器与URI的某个段关联?

2) What are the best practices to work with controllers? 2)与控制器配合使用的最佳实践是什么? I mean, how can I avoid dumping all my methods in a single controller? 我的意思是,如何避免将所有方法转储到单个控制器中? Can I split a controller in several files without creating unnecessary URI. 我可以在不创建不必要的URI的情况下将控制器分为几个文件吗?

1.Yes controller always associate to segment of a URI. 1.是的,控制器始终与URI段关联。 If your controller is under some directory like 如果您的控制器在某个目录下,例如

controllers 
    search ---------------------directory inside controller
       search ------------------controller
           stock_search -------------------method

then it will add whole path in the uri segment eg :basepath.'search/search/stock_search/'; 然后它将在uri段中添加整个路径,例如:basepath.'search / search / stock_search /';

But you can route it your custom path using routes.php $route['search'] = 'search/search/stock_search/'; 但是您可以使用routes.php $ route ['search'] ='search / search / stock_search /'将其路由为您的自定义路径;

2.You can create different controllers (name should be different) with the different methods or you can say you can split controller methods in different files and customise their url accordingly in routes.php and can create parent controller to use methods in any controller by extending through it. 2.您可以使用不同的方法创建不同的控制器(名称应不同),或者可以说您可以将控制器方法拆分到不同的文件中,并在routes.php中相应地自定义其URL,并且可以创建父控制器以使用任何控制器中的方法通过它延伸。

If you want to get something in codeigniter then codeigniter send the request to a controller. 如果您想在codeigniter中获得一些东西,那么codeigniter会将请求发送到控制器。 URI must have a controller if no controller in uri then the reguest is goes to default controller which is tell in application/config/routes.php in this code $route['default_controller'] = 'welcome'; 如果uri中没有控制器,则URI必须具有一个控制器,然后要求转到默认控制器,该默认控制器在此代码中的application/config/routes.php中告诉$route['default_controller'] = 'welcome';

And will not be able to split a controller in several files without creating more than one URI. 如果不创建多个URI,将无法将控制器拆分为多个文件。

Controller is associated to url segments. 控制器与url段关联。

Url used in Codeigniter is as follows: http://example.com/index.php/projname/controller/method/params . Codeigniter中使用的网址如下: http://example.com/index.php/projname/controller/method/params : http://example.com/index.php/projname/controller/method/params

If you dont specify controller in uri, default controller is called specified in routes.php $route['default_controller'] = 'welcome'; 如果您未在uri中指定控制器,则会在route.php中指定默认控制器。 $route['default_controller'] = 'welcome';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM