Danny
Danny

Reputation: 4754

Making static pages in Symfony 1

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?

Upvotes: 5

Views: 5393

Answers (6)

S.Petrykowski
S.Petrykowski

Reputation: 21

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.

Upvotes: 0

chrismacp
chrismacp

Reputation: 3893

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.

Upvotes: 2

nacmartin
nacmartin

Reputation: 2182

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.

Upvotes: 0

gpilotino
gpilotino

Reputation: 13302

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)

Upvotes: 4

Felix Kling
Felix Kling

Reputation: 816272

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.

Upvotes: 3

richsage
richsage

Reputation: 27102

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.

Upvotes: 4

Related Questions