在 Codeigniter 中配置 routes.php
configuring routes.php in Codeigniter
我刚刚开始一个新项目,但我被困在 Codeigniter 的 URI 配置中。
我正在使用 xampp,到目前为止,我有一个包含我网站的文件夹,路径如下:
c:/xampp/htdocs/new_admin/site/application
c:/xampp/htdocs/new_admin/site/system
正如 Codeigniter 手册中所说,我在 config.php 基础 URL 中添加了我认为是这样的:
$config['base_url'] = 'http://localhost/new_admin/';
我把index_page留空了:
$config['index_page'] = '';
我希望我的 URI 看起来像:
http://localhost/new_admin/[controller]/[method]
例如:
http://localhost/new_admin/admin/index
所以在文件 routes.php 中我这样做了:
$route['default_controller'] = 'admin';
$route['(:any)'] = 'admin/index';
但它甚至没有显示方法索引的结果。错误是 404.
由于我在文档中找不到解决方案,我想我遗漏了一些东西。
我该怎么做才能让这个工作如我所愿?
更新:我在 .htaccess 文件中写了这个
#Deny from all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
ErrorDocument 404 /index.php
所以经过一天的尝试,我发现我在
中配置我的虚拟主机是错误的
c:/xampp/apache/conf/extra/httpd-vhosts.conf
这是它现在的工作方式:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "C:/xampp/htdocs/new_admin/site"
ServerName local.new_admin
</VirtualHost>
<Directory "C:/xampp/htdocs/new_admin/site">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
# to allow access from my local network add this line so other PC on my network are allowed in but not anything from the internet
# If you want the world to be allowed in uncomment this line
allow from all
</Directory>
然后
c:/windows/system32/drivers/etc/hosts
我添加了这行:
127.0.0.1 localhost
127.0.0.1 local.new_admin #this is my site
修复后,我在此目录中添加了一个新的 .htaccess 文件:
c:/xampp/htdocs/new_admin/site/
其中包含:
php_value memory_limit 64M
php_flag magic_quotes_gpc 0
php_flag magic_quotes_runtime 0
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/ [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/ [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
在Codeigniter的应用程序文件夹和系统文件夹旁边,为了添加mod_rewrite权限
现在要在本地访问我的项目,我只需键入:
http://local.new_admin/admin/index
希望这对像我这样的新手有用。
我刚刚开始一个新项目,但我被困在 Codeigniter 的 URI 配置中。
我正在使用 xampp,到目前为止,我有一个包含我网站的文件夹,路径如下:
c:/xampp/htdocs/new_admin/site/application
c:/xampp/htdocs/new_admin/site/system
正如 Codeigniter 手册中所说,我在 config.php 基础 URL 中添加了我认为是这样的:
$config['base_url'] = 'http://localhost/new_admin/';
我把index_page留空了:
$config['index_page'] = '';
我希望我的 URI 看起来像:
http://localhost/new_admin/[controller]/[method]
例如:
http://localhost/new_admin/admin/index
所以在文件 routes.php 中我这样做了:
$route['default_controller'] = 'admin';
$route['(:any)'] = 'admin/index';
但它甚至没有显示方法索引的结果。错误是 404.
由于我在文档中找不到解决方案,我想我遗漏了一些东西。
我该怎么做才能让这个工作如我所愿?
更新:我在 .htaccess 文件中写了这个
#Deny from all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
ErrorDocument 404 /index.php
所以经过一天的尝试,我发现我在
中配置我的虚拟主机是错误的c:/xampp/apache/conf/extra/httpd-vhosts.conf
这是它现在的工作方式:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "C:/xampp/htdocs/new_admin/site"
ServerName local.new_admin
</VirtualHost>
<Directory "C:/xampp/htdocs/new_admin/site">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
# to allow access from my local network add this line so other PC on my network are allowed in but not anything from the internet
# If you want the world to be allowed in uncomment this line
allow from all
</Directory>
然后
c:/windows/system32/drivers/etc/hosts
我添加了这行:
127.0.0.1 localhost
127.0.0.1 local.new_admin #this is my site
修复后,我在此目录中添加了一个新的 .htaccess 文件:
c:/xampp/htdocs/new_admin/site/
其中包含:
php_value memory_limit 64M
php_flag magic_quotes_gpc 0
php_flag magic_quotes_runtime 0
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/ [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/ [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
在Codeigniter的应用程序文件夹和系统文件夹旁边,为了添加mod_rewrite权限
现在要在本地访问我的项目,我只需键入:
http://local.new_admin/admin/index
希望这对像我这样的新手有用。