直接链接到子文件夹视图返回404 [英] Direct link to a subfolder view returns 404

查看:218
本文介绍了直接链接到子文件夹视图返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法直接加载我的CodeIgniter views 文件夹中具有直接链接的子文件夹中的视图。



这里是文件夹结构:

  my_project / 
application /
views /
pages /
about.php
about /
organization.php
history.php

我需要通过链接访问 organization.php history.php

 < div id =container> 
< ul>
< li>
< a href ='<?php echo base_url('about'); ?>'>关于< / a>
< / li>
< li>
< a href ='<?php echo base_url('about / organization'); ?>'>关于/组织< / a>
< / li>
< li>
< a href ='<?php echo base_url('about / history'); ?>'>关于/历史< / a>
< / li>
< / ul>
< / div>

但我得到了一个404。



我的 htaccess

 < IfModule mod_rewrite.c& 
RewriteEngine On
RewriteBase / my_project /

#从ExpressionEngine URL中删除index.php
RewriteCond%{THE_REQUEST} ^ GET。* index\.php [NC ]
RewriteRule(。*?)index\.php /*(.*)/ my_project / $ 1 $ 2 [R = 301,NE,L]

#指示所有EE Web请求通过网站索引文件
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule ^(。*)$ /my_project/index.php/$1 [L ]
< / IfModule>

这是我的 routes.php 到我的默认视图控制器 Pages :: view(path / to / file)

  $ route ['default_controller'] ='welcome'; 
$ route ['404_override'] ='';
$ route ['translate_uri_dashes'] = FALSE;

$ route ['pages /(:any)/(:any)'] ='pages / view / $ 1 / $ 2';
$ route ['(:any)'] ='pages / view / $ 1';

这是我的控制器:

 <?php 
类扩展CI_Controller
{
function __construct()
{
parent :: __ construct
}

public function view($ page ='home')
{
if(!file_exists(APPPATH.'views / pages /'.$ page。 '.php'))
{
show_404();
}

$ data ['title'] = strtoupper(str_replace([ - , - ],'',$ page)
$ this-> load-> view('templates / header',$ data);
$ this-> load-> view('pages /'.$ page,$ data);
$ this-> load-> view('templates / footer',$ data);
}
}






每当我直接使用时,

  $ this-> load-& views / about / organization'); 

没关系。与打印相同:

  file_exists(APPPATH.'views / pages / about / organization.php'); 

我不知道有什么可能弄乱了。



我必须为这些子文件夹制作自定义控制器吗?这是我现在可以想到的唯一解决方案。






注意我还将此子文件夹(整个项目的实际)权限设置为 777 为每个子文件夹分别分隔控制器,只是为了查看它们!这是真的不可能只做一个控制器吗?



类似于url模式 my_app / controller / method 是严格的, my_app /除非文件夹视图对应于,否则不允许查看控制器方法






解决方案



Zeeshan < a href =http://66.media.tumblr.com/f19127770632190559739b5b3fa5c2fd/tumblr_inline_nzp01iXO3M1sb92kb_500.gif =nofollow>



Shantay,你留下! 谢谢



您是正确的,我要使用通用方法捕获我的所有静态页面查看请求。我们现在可以关闭这个问题(我将在20小时内给予奖金。)



你是唯一理解作业的人

解决方案

经过你的问题,我发现你正在尝试写一些通用的函数,将负责加载你的意见。请参考下面,因为你可以处理任何



控制器函数将如下所示:

 函数视图($ folder ='',$ page ='home')
{
$ path = $ folder!=?
$ path = $ page!=?$ path。$ page:$ page;

$ this-> load-> view('templates / header',$ data);
** $ this-> load-> view($ path,$ data); **
$ this-> load-> view('templates / footer',$ data) ;
}

现在在您的视图中,链接将如下所示:

 < div id =container> 
< ul>
< li>
< a href ='<?php echo base_url。about /; ?>'>关于< / a>
< / li>
< li>
< a href ='<?php echo base_url。about / organization?>'>关于/ Organization< / a>
< / li>
< li>
< a href ='<?php echo base_url。about / history?>'>关于/历史< / a>
< / li>
< / ul>



不需要手动处理404.Just添加以下以显示您的自定义

  $ route ['404_override'] ='mycustom404 '; // mycustom404是404的控制器

如果您有任何疑问或疑问, / p>

尊敬的,
Zeeshan。


I cannot directly load a view that is in a subfolder in my CodeIgniter views folder with a direct link.

Here is the folder structure:

my_project/
    application/
        views/
            pages/
                about.php
                about/
                    organization.php
                    history.php

I need to access organization.php and history.php through a link such as:

<div id="container">
    <ul>
        <li>
            <a href='<?php echo base_url('about'); ?>'>About</a>
        </li>
        <li>
            <a href='<?php echo base_url('about/organization'); ?>'>About/Organization</a>
        </li>
        <li>
            <a href='<?php echo base_url('about/history'); ?>'>About/History</a>
        </li>
    </ul>
</div>

However I get a 404.

My htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my_project/

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /my_project/$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_project/index.php/$1 [L]
</IfModule>

Here is my routes.php that directs to my default view controller Pages::view(path/to/file):

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['pages/(:any)/(:any)'] = 'pages/view/$1/$2';
$route['(:any)'] = 'pages/view/$1';

Here is my controller:

<?php
class Pages extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function view($page = 'home')
    {
        if(!file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
            show_404();
        }

        $data['title'] = strtoupper(str_replace(["-", "–"], ' ', $page));
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }
}


NOTE whenever I directly use

$this->load->view('pages/views/about/organization');

it is okay. Same with printing:

file_exists(APPPATH.'views/pages/about/organization.php');

I don't know what could have messed this up.

Do I have to make a custom controller just for these subfolders? This is the only solution I can think of right now.


NOTE I also set this subfolder's (the entire project's actually) permissions to 777 and the direct link still did not work.


NOTE My only solution right now is to make separate controllers for each of these subfolders just to view them! Is this really impossible to do with just one controller?

Like the url pattern my_app/controller/method is strict and my_app/folder/view is not allowed unless folder and view corresponds to a controller and method respectively?


SOLUTION

Zeeshan did that!

Shantay, you stay! Thank you.

You are correct, I was going for a generic method to catch all my static page view requests. We can close this issue now (I will award the bounty in 20 hours).

You were the only one who understood the assignment.

解决方案

After going through your question,i found that you are trying to write some generic function that will take care of loading your views.Please refer below as you can handle any structure in codeigniter.

Controller function will go like this

public function view($folder='',$page = 'home')
{       
    $path = $folder!=""?$folder.'/':'';
    $path = $page!=""?$path.$page:$page;

    $this->load->view('templates/header', $data);
    **$this->load->view($path, $data);**
    $this->load->view('templates/footer', $data);
}

Now in your views the link will go like this:

<div id="container">
<ul>
    <li>
        <a href='<?php echo base_url."about/"; ?>'>About</a>
    </li>
    <li>
        <a href='<?php echo base_url."about/organization" ?>'>About/Organization</a>
    </li>
    <li>
        <a href='<?php echo base_url."about/history" ?>'>About/History</a>
    </li>
</ul>

Also please note in Codeigniter you don't need to handle 404 manually.Just add following to show your custom

$route['404_override'] = 'mycustom404';//mycustom404 is controller for 404

Let me know if you have any doubts or queries.

Regards, Zeeshan.

这篇关于直接链接到子文件夹视图返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆