您如何创建“content_for” PHP 中的等效项?

发布于 2024-10-07 12:16:28 字数 1046 浏览 3 评论 0原文

我一直在用 PHP 开发一个小页面,该页面不需要背后有成熟框架的力量。我在 Ruby-on-Rails 之前的工作中真正缺少的一件事是使用“content_for”有效地将内容传递到页面的能力。

我想知道的是,如何创建一个页面生命周期来在 PHP 中实现同样的效果?

因此,这是一个简单的示例:

假设您有一个定义索引页面的模板,只是一个要在所有页面上使用的重复标题和菜单。因此,您的 index.php 文件基本上如下所示:

...header stuff...
<body>
<?php include $file.'.php'; ?>
</body>
...footer stuff...

编辑:感谢有关 URL 安全性的提示,但我们假设我安全地获取了用户请求:)

现在,让我们在标头中说想要这样写:

<head>
<title><?php echo $page_title; ?></title>
</head>

如果能够在包含的文件中指定标题,那就太好了,因此在 url http://example.com/index.php?p=test 处,您可以加载 test.php,该文件如下所示:

<?php $page_title = 'Test Page'; ?>
... rest of content ...

现在,显然这不起作用,因为包含页面 (index.php) 是在设置变量之前加载的。

在 Rails 中,您可以使用 content_for 函数在“页面上”传递内容。

我的问题是:你们能想到的在 PHP 中实现这种“content_for”功能的最简单、最精简的方法是什么?

理想情况下,我想要不涉及捆绑的建议在一些大型框架上,但有一些相对较轻的样板代码,可以在很多不同的应用程序中使用。

I've been working on a small page in PHP, one that doesn't need the power of a full-fledged framework behind it. One thing that I'm really missing from previous work in Ruby-on-Rails is the ability to effectively pass content up the page using "content_for".

What I was wondering is, how could you create a page lifecycle that would accomplish this same effect in PHP?

So, here's a simple example:

Let's say you have a template that defines an index page, just a recurring header and menu you want to use on all your pages. So your index.php file looks basically like this:

...header stuff...
<body>
<?php include $file.'.php'; ?>
</body>
...footer stuff...

EDIT: Thanks for the tips on URL security, but let's just assume I'm getting the user request safely :)

Now, lets say in the header you want to put this:

<head>
<title><?php echo $page_title; ?></title>
</head>

It would be nice to be able to specify the title in the included file, so at the url http://example.com/index.php?p=test you're loading test.php, and that file looks like this:

<?php $page_title = 'Test Page'; ?>
... rest of content ...

Now, obviously this doesn't work, because the including page (index.php) is loaded before the variable is set.

In Rails this is where you could pass stuff 'up the page' using the content_for function.

My question is this: What would be the simplest, leanest way that you all can think of to effect this kind of 'content_for' functionality in PHP?

Ideally I'd like suggestions that don't involve strapping on some big framework, but some relatively light boilerplate code that could be used in a lot of different applications.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洋洋洒洒 2024-10-14 12:16:28
  1. 永远不要包含 $_GET['p']。这会在您的网站中打开一个巨大的安全漏洞,因为 include 接受文件名和 URL,因此任何人都可以读取您网站上的任何文件,并在您的服务器上执行任何代码。您可能需要先检查并清理该值。

  2. 如果您需要简单的东西,您可以将页眉和页脚放在单独的文件中,执行您的 test.php 这将设置变量,使用 输出缓冲,然后包含页眉,输出中间部分并包含页脚。示例:

     
     <正文>
     
     
     
    
  1. Never do include $_GET['p']. This opens a huge security hole in your site, as include accepts filenames and URLs, so anybody would be able to read any file on your site and also execute any code on your server. You may want to check and sanitize the value first.

  2. If you need something simple, you may put header and footer in separate files, execute your test.php which would set the variables, capture its output using output buffering, then include the header, output the middle part and include the footer. Example:

     <?php ob_start(); ?>
     <body>
     <?php include $filename.'.php'; ?>
     </body>
     <?php $content = ob_get_clean(); 
      include 'header.php';
      echo $content;
      include 'footer.php';
      ?>
    
差↓一点笑了 2024-10-14 12:16:28

如果我理解正确(我没有广泛使用 RoR),您可以将数据放入变量或函数中。如果您的内容位于变量中,您的“test.php”可以简单地保存所有变量,并且您可以在索引文件的最开头加载它(对于函数也同样取决于您的需求的复杂程度;如果您是做了很多额外的工作,你可能需要使用函数作为变量是行不通的)。

例如,您的 test.php 看起来像这样:

<?php
$page_title = "Test Page";
$page_content = "Some sort of content";

// Or

function page_content()
{
    // Run some functions and print content at the end
}

?>

然后,在您的 index.php 中,

<?php include $_GET['p'].'.php'; ?>
...header stuff...
<title><?php print $page_title; ?></title>
<body>
<?php print $page_content; ?>
<!-- OR if function -->
<?php page_content(); ?>
</body>
...footer stuff...

这样所有内容都应该正确加载。您也可以将事物分开,但这会使您的结构复杂化(特别是如果不需要复杂的框架,则这是不必要的)。

祝你好运!
丹尼斯·M.

If I understand you correctly (I have not used RoR extensively), you could put your data in a variable or a function. If your content was in a variable, your "test.php" could simply hold all your variables and you could load it at the very beginning of your index file (likewise for a function depending on how complicated your needs are; if you're doing a lot of extra work, you may need to use a function as a variable won't work).

For example, your test.php would look something like this:

<?php
$page_title = "Test Page";
$page_content = "Some sort of content";

// Or

function page_content()
{
    // Run some functions and print content at the end
}

?>

Then, in your index.php

<?php include $_GET['p'].'.php'; ?>
...header stuff...
<title><?php print $page_title; ?></title>
<body>
<?php print $page_content; ?>
<!-- OR if function -->
<?php page_content(); ?>
</body>
...footer stuff...

This way everything should load properly. You could also split things up, but that would complicate your structure (especially if there is no need for an elaborate framework, this would be unnecessary).

Good luck!
Dennis M.

小伙你站住 2024-10-14 12:16:28

您担心 XSS 吗?或者您要从查询字符串中过滤/将“文件名”列入白名单吗?

我的答案是使用 mod_rewrite —— 如果您使用 PHP,那么您很可能使用 Apache!

您可以使用 RewriteCond 过滤掉文件,您的 RewriteRule 可以是:

RewriteRule /index.php?p=(.*)$ $1 [L,QSA]

这可能是与您正在寻找的 PHP 功能不同的方法,但它我想到...

Are you worried about XSS? Or are you going to filter/whitelist the "filenames" from the query string?

My answer would be to use mod_rewrite -- if you're using PHP, you're likely using Apache!

You could filter out files with a RewriteCond and your RewriteRule could be:

RewriteRule /index.php?p=(.*)$ $1 [L,QSA]

This may be a different approach than the PHP functionality you were looking for, but it comes to mind...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文