5

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?

Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68
Danny
  • 4,724
  • 6
  • 42
  • 55

6 Answers6

4

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.

richsage
  • 26,912
  • 8
  • 58
  • 65
  • Thanks! It works but how do i edit the routing.yml files so that i can access the page like host.com/about – Danny Jan 08 '10 at 02:28
  • Great, glad it worked for you. In your routing.yml, you can add a route that just matches the 'action' part and always uses the static module. I've put an example up at http://pastebin.com/f6b55d7a7 – richsage Jan 08 '10 at 09:19
4

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)

gpilotino
  • 13,055
  • 9
  • 48
  • 61
3

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.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
2

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.

chrismacp
  • 3,834
  • 1
  • 30
  • 37
0

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.

nacmartin
  • 2,172
  • 18
  • 15
0

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.