更改 MVC 中的特定路线 CI

Change Particular route in MVC CI

我在 PHP MVC CI

的示例应用程序中有以下 Url
http://localhost:1234/Sample/Index.php/User/index

此处用户为 Controller,索引为 Action Method

我可以像下面这样写 url 吗?

http://localhost:1234/Users

如果您想使用自定义路线。 http://www.codeigniter.com/user_guide/general/routing.html

您不需要在 routes.php

中包含 index.php
// application > controllers > sample > Users.php filename first letter must be uppercase. Same with class name.
$routes['users'] = 'sample/users/index'; // Lower case preferred.

// When with out function would go straight to index function
$routes['users'] = 'sample/users'; 


// No Need for index.php
// $routes['Users'] = 'Sample/Index.php/User/index';

现在 url 将是

http://localhost:1234/project/users

http://localhost/project/users

或 index.php

http://localhost:1234/project/index.php/users

http://localhost/project/index.php/users

您可能需要删除 index.php 这里有一些 htaccess

https://github.com/riwakawebsitedesigns/htaccess_for_codeigniter

应用程序 > 配置 > config.php

$config['index_page'] = '';

htaccess 示例

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond  !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L,QSA]

请注意 WAMP 用户:

如果您使用 WAMP,请确保您已启用重写模块。

Sample是你的项目目录所以你可以不写你想要的路线你可以写下面的路线。

http://localhost:1234/Sample/Users

您的路线文件将如下所示。

$route['Users'] = 'User/index';

看到你的代码后,你犯的主要错误是重命名了 index.php,这是错误的,请将 index1212.php 恢复为 index.php

其余的你可以按照其他answers.But这是maon,codeigniter不会运行没有index.php文件

编辑

我在 routes.php

中注意到另一个错误

$route['Users'] = 'user/';

不是$routes

并像这样访问您的项目,http://localhost/Sample/Users

据我所知,您无法在 url 中隐藏文件夹名称。