无法路由到 CodeIgniter 中的静态 PHP 页面
Unable to route to static PHP page in CodeIgniter
我是 CodeIgniter 的新手。在我的 'views' 文件夹中,我创建了 2 个 PHP 页 - home.php 和 register.php。我还在模板文件夹中创建了页眉和页脚 PHP 页面。
这是我的 pages.php 控制器代码 class:
<?php
class Pages extends CI_Controller
{
public function view($page)
{
if(!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
这是我的 routes.php 代码:
$route['register'] = "pages/view/register";
$route['default_controller'] = 'pages/view/home';
我还没有在 .htaccess 文件中写入任何内容。
我的项目主目录是'ED'
我可以通过 URL 导航到主页:http://localhost/ED
但是我无法使用这些 URL 导航到注册页面:
localhost/ED/register
或
localhost/ED/register.php
请帮助我如何实现这一目标。
默认情况下你必须有 index.php 因为根据 CodeIgniter URLs
By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article
所以如果你想删除index.php,你可以按照这个简单的步骤:
You can easily remove this file by using a .htaccess file with some
simple rules. Here is an example of such a file, using the "negative"
method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/ [L]
In the above example, any HTTP
request other than those for index.php, images, and robots.txt is
treated as a request for your index.php file.
只需将您的 .htaccess 文件(使用 mod 重写)放入您的主应用程序文件夹中,瞧,它就完成了。
我用我的 .htaccess 做的是这个:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /name_of_your_codeigniter_folder/
#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]
</IfModule>
<IfModule !mod_rewrite.c>
# 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
</IfModule>
我是 CodeIgniter 的新手。在我的 'views' 文件夹中,我创建了 2 个 PHP 页 - home.php 和 register.php。我还在模板文件夹中创建了页眉和页脚 PHP 页面。
这是我的 pages.php 控制器代码 class:
<?php
class Pages extends CI_Controller
{
public function view($page)
{
if(!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
这是我的 routes.php 代码:
$route['register'] = "pages/view/register";
$route['default_controller'] = 'pages/view/home';
我还没有在 .htaccess 文件中写入任何内容。
我的项目主目录是'ED'
我可以通过 URL 导航到主页:http://localhost/ED
但是我无法使用这些 URL 导航到注册页面: localhost/ED/register
或
localhost/ED/register.php
请帮助我如何实现这一目标。
默认情况下你必须有 index.php 因为根据 CodeIgniter URLs
By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article
所以如果你想删除index.php,你可以按照这个简单的步骤:
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/ [L]
In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.
只需将您的 .htaccess 文件(使用 mod 重写)放入您的主应用程序文件夹中,瞧,它就完成了。 我用我的 .htaccess 做的是这个:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /name_of_your_codeigniter_folder/
#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]
</IfModule>
<IfModule !mod_rewrite.c>
# 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
</IfModule>