简体   繁体   English

PHP函数参数不能正常工作?

[英]PHP function parameters don't work properly?

I'm trying to set up a website using MVC, but I'm stuck at the very beginning. 我正在尝试使用MVC来建立网站,但是一开始我还是很困惑。

In project0/application/controllers I have a pages.php file which looks like: 在project0 / application / controllers中,我有一个pages.php文件,看起来像:

<?php

class Pages extends CI_Controller {

public function view($page = 'home')
{

if (!file_exists('application/views/pages/'.$page.'.php'))
{
    // Whoops, we don't have a page for that!
    show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);

}
}

and in project0/application/views/pages two files named home.php and about.php, containing a "Hello world". 在project0 / application / views / pages中,有两个文件名为home.php和about.php,其中包含“ Hello world”。

When I want to visit http://project0/index.php/pages/view/about , it gives me the 404 error: it seems the home.php or about.php files are not existing. 当我想访问http://project0/index.php/pages/view/about时 ,它给了我404错误:似乎home.php或about.php文件不存在。

Can somebody help me out? 有人可以帮我吗?

In your script, try a getcwd(); 在脚本中,尝试使用getcwd(); and then double check the results from that (your current working directory) against the relative path 'application/views/pages/'.$page.'.php' . 然后对照相对路径'application/views/pages/'.$page.'.php'仔细检查该结果(您当前的工作目录)。 I would guess that you're not looking for your page in the directory you think you are. 我猜您不是在您认为的目录中寻找页面。

You just solved your own error. 您只是解决了自己的错误。 Your pages home.php and about.php are located in: project0/application/views 您的页面home.php和about.php位于: project0/application/views

But the code you posted is looking for them in 但是您发布的代码正在其中寻找它们

project0/application/views/pages/

I think whether your code will work depends on your setup (server setup and where your actual script runs from), but you can use one of several methods to make it more foolproof. 我认为您的代码是否有效取决于您的设置(服务器设置以及实际脚本的运行位置),但是您可以使用以下几种方法之一使其更加安全。

You can use the getcwd() function: 您可以使用getcwd()函数:

if (!file_exists(getcwd().'application/views/pages/'.$page.'.php'))

Or you can define a permanent site path in your main config files, for instance: 或者,您可以在主配置文件中定义永久站点路径,例如:

$config['path'] = '/home/project0/';

... ...

if (!file_exists($config['path'].'application/views/pages/'.$page.'.php'))

尝试指定绝对路径,而不是相对的'application/views/pages/'.$page.'.php'它可能无法正常工作。

You are probably checking for the file in the wrong place, I believe file_exists is looking for that path relative to project0/application/controllers . 您可能正在错误的位置检查文件,我相信file_exists正在寻找相对于project0/application/controllers路径。 Try this: 尝试这个:

if (!file_exists('../views/pages/'.$page.'.php'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM