开发者

Basic question about CodeIgniter URL

开发者 https://www.devze.com 2023-03-09 08:13 出处:网络
When I create a controller in CodeIgniter, for example \"login.php\". I create a public function \"index\" in it, and load a view \"login_form\" in the function, the CodeI开发者_StackOverflow中文版gni

When I create a controller in CodeIgniter, for example "login.php". I create a public function "index" in it, and load a view "login_form" in the function, the CodeI开发者_StackOverflow中文版gniter generates the url localhost/test/login.

Now, If i create another function "register" in the same Controller, the url will be localhost/test/login/register, but I want to create url like localhost/test/register. Do I need to create a new controller register and make the index function in that, or same URL can be generated by adding function in login controller. What is the 'usual' way to do it? Thanks.


As mentioned you can (and most likely should) use a route for this:

// config/routes.php
$route['register'] = 'test/register';

This is basically saying: If /register is requested, give them /test/register.

Note that /test/register is still valid. If you want to disable it for some reason, you can do this:

$route['test/register'] = FALSE; // Or map to your 404 page

...but in general you won't need to.

Note that use of routes does not magically rewrite your links or anything, it just routes the request somewhere else when it is made.

Remapping is quite cool, but what you're asking is definitely a job for a route. Read the user guide carefully, there's a lot you can do with them.


You can use one of the following to achieve desired outcome:

  • http://codeigniter.com/user_guide/general/routing.html
  • http://codeigniter.com/user_guide/general/controllers.html#remapping

More information about MVC can be found here


I would suggest reading this guide on how to extend your controller (in this case register) with login check. That would make you have protected code in the index function of your register controller. And in any other controllers you may want to add. I think in your approach now you are in the risk of having one enourmous login controller instead of having several smaller controllers with a bit more structure and overview.

0

精彩评论

暂无评论...
验证码 换一张
取 消