在 Symfony 1 中制作静态页面

发布于 2024-08-16 23:52:56 字数 126 浏览 3 评论 0原文

我是 symfony 的新手,有一些简单的问题。我试图了解模块系统,但我不明白如何创建实际的主页或其他不基于数据库模型的页面。例如,具有静态信息的简单“关于”页面或由来自不同模型的一堆信息组合而成的主页。

有人可以帮忙吗?

Im new to symfony and have some simple questions. I am trying to understand the module system, but I dont understand how I create the actual homepage or other pages that are not based off of a model from the db. For example, the simple about page that has static info or the homepage that is a combination of a bunch of information from different models.

Can anyone help?

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

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

发布评论

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

评论(6

尐偏执 2024-08-23 23:52:56

首先,模块不必局限于数据库中的模型。您可以拥有一个不依赖于数据库内容的 Foo 模块和一个主要基于 3 个不同模型的 Bar 模块。模块分离是一种将站点逻辑地分解为可管理部分的方法。例如,电子商务网站可能有产品模块、类别模块和购物车模块等。

然后,您的最后一句话可以分为两部分:

1)静态信息可以位于任何页面上 - 如果是“关于我们”和“常见问题解答”等内容,我个人倾向于使用“默认”或“主页”模块,并在其中创建各种操作:

./symfony generated:module appname home

以及

class homeActions extends sfActions
{
  public function executeAbout(sfWebRequest $request)
  {
    // ...
  }

  public function executeFaq(sfWebRequest $request)
  {
    // ...
  }
}

相应的模板文件(aboutSuccess.php、faqSuccess.php)。

2) 一个页面可以由来自许多不同模型的数据组成 - 只需使用您喜欢的 ORM 检索数据的方法并将其设置到视图 ($this->data = MyModel->findByColumn(...) 等)。如果您指的是来自不同模块的数据,那么您最好查看可在不同模块(导航等)之间使用的页面元素的部分或组件。有关更多详细信息,请参阅 Symfony 文档这些。

First of all, modules do not have to be restricted to a model from the database. You can have a Foo module which relies on no database content, and a Bar module that is primarily based on 3 different models. The module separation is a way to logically break up your site into manageable sections. Eg an e-commerce site might have a Products module, a Categories module and a Cart module and so on.

Your last sentence can then be split into 2 parts:

1) Static information can be on any page - if it's for things like "About us" and "FAQ" etc, I personally tend to use a "default" or "home" module, and create the various actions in there vis:

./symfony generate:module appname home

and

class homeActions extends sfActions
{
  public function executeAbout(sfWebRequest $request)
  {
    // ...
  }

  public function executeFaq(sfWebRequest $request)
  {
    // ...
  }
}

with the corresponding template files (aboutSuccess.php, faqSuccess.php).

2) A page can be comprised of data from many different models - just use your preferred ORM's method of retrieving data and set it to the view ($this->data = MyModel->findByColumn(...) etc). If you mean data from different modules, then you'd probably be better off looking at partials or components for elements of a page that can be used across different modules (navigation etc). See the Symfony docs for more details on these.

安人多梦 2024-08-23 23:52:56

我习惯用这种方式处理静态页面。

首先,我在 apps/frontend/config/routing.yml 中创建一个新条目:

page:
  url:   pages/:page
  param: { module: page, action: index }

然后编写一个“页面”模块(apps/frontend/modules/page/actions/actions.class.php):

<?php
class pageActions extends sfActions
{
  public function executeIndex()
  {
    $this->page = $this->getRequestParameter("page");
    $this->forward404Unless($this->_partialExists($this->page));
  }

  protected function _partialExists($name)
  {
    $directory = $this->getContext()->getModuleDirectory();
    return (is_readable($directory.DIRECTORY_SEPARATOR."templates".
            DIRECTORY_SEPARATOR."_".$name.".php"));
  }
}

最后一步,放入 module/ page/templates/indexSuccess.php 这段代码:

<?php include_partial($page); ?>

所以从现在开始你所要做的就是为每个静态页面创建一个部分,即。
apps/frontend/modules/page/templates/_home.php 您可以访问
http://yousite/pages/home (无需为每个页面添加新的路由条目)

I'm used to handle static pages in this way.

First I create a new entry in apps/frontend/config/routing.yml:

page:
  url:   pages/:page
  param: { module: page, action: index }

Then I write a "page" module (apps/frontend/modules/page/actions/actions.class.php):

<?php
class pageActions extends sfActions
{
  public function executeIndex()
  {
    $this->page = $this->getRequestParameter("page");
    $this->forward404Unless($this->_partialExists($this->page));
  }

  protected function _partialExists($name)
  {
    $directory = $this->getContext()->getModuleDirectory();
    return (is_readable($directory.DIRECTORY_SEPARATOR."templates".
            DIRECTORY_SEPARATOR."_".$name.".php"));
  }
}

Last step, put in modules/page/templates/indexSuccess.php this code:

<?php include_partial($page); ?>

So all you have to do from now is to create a partial for each static page ie.
apps/frontend/modules/page/templates/_home.php which you can reach at
http://yousite/pages/home (without the need to add a new routing entry for every page)

孤檠 2024-08-23 23:52:56

您可以创建一个模块(例如,称为static),并为每个静态页面创建操作,或者仅创建一个根据请求变量传递页面的操作。此操作唯一要做的就是加载模板。

恕我直言,如果 symfony 附带一个默认模块就好了。

例如(我的自定义)模块 static 的操作:

class staticActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    if(!$request->hasParameter('site')) {
        return sfView::ERROR;    
    }
    $this->site = $request->getParameter('site');
  }
}

使用此模板:

//indexSuccess.php
<?php include_partial($site) ?>

实际的静态站点都是部分的。

在我的 routing.yml 中,看起来像这样:

# static stuff
about:
  url: /about
  param: {module: static, action: index, site: about}

这样,当您添加静态站点时,您只需创建一个新的部分和一个新的路由条目,而不必接触 PHP 代码。

You can create a module, e.g. called static and create actions for every static page or only one action that delivers the page depending on a request variable. The only thing this action does is loading a template.

IMHO it would be good if symfony comes with a default module for this.

For example actions of (my custom) module static:

class staticActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    if(!$request->hasParameter('site')) {
        return sfView::ERROR;    
    }
    $this->site = $request->getParameter('site');
  }
}

With this template:

//indexSuccess.php
<?php include_partial($site) ?>

The actual statics sites are all partials.

In my routing.yml looks like this:

# static stuff
about:
  url: /about
  param: {module: static, action: index, site: about}

This way you only have to create a new partial and a new routing entry when you add a static site and you don't have to touch the PHP code.

﹉夏雨初晴づ 2024-08-23 23:52:56

另一种无需编写任何控制器代码即可提供静态页面的方法是设置如下所示的路由:

myStaticPage:
    pattern: /pageName
    defaults:
        _controller: FrameworkBundle:Template:template
        template: MyBundle:Home:pageName.html.twig

然后只需创建您的树枝模板,它应该可以正常工作。

Another way to serve static pages without having to write any controller code is to set up the route something like the following:

myStaticPage:
    pattern: /pageName
    defaults:
        _controller: FrameworkBundle:Template:template
        template: MyBundle:Home:pageName.html.twig

Then just create your twig template and it should work fine.

大海や 2024-08-23 23:52:56

除了上述内容之外,还可以考虑为静态页面建立一个 CMS,这样您就不需要精通技术的人员来维护或更改它们。当然,这取决于项目。

Apart from the above, consider having a CMS for static pages, so you won't need technical savy people to mantain them or change them. This depends on the project, of course.

时光是把杀猪刀 2024-08-23 23:52:56

对于真正静态且独立的页面,您只需在[pathToYourProjectRoot]/web 目录中创建任何文件即可。

它可能是[pathToYourProjectRoot]/web/assets/static_html/about.html
然后通过http://your.site.com/assets/static_html/about.html直接链接到该页面。

For really static and independent pages you can simply create any file in [pathToYourProjectRoot]/web directory.

It may by i.e. [pathToYourProjectRoot]/web/assets/static_html/about.html.
Then link to the page directly by http://your.site.com/assets/static_html/about.html.

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