官术网_书友最值得收藏!

Creating a page type

Creating pages dynamically is a central concept of concrete5 development and can unlock a lot of powerful features in custom applications. Imagine a concrete5 website that has a recipes database. You could make it so, that every time a recipe is added to the database, a new page for that recipe is added to the sitemap, immediately improving the depth of content on your website, its usability, and even search engine performance.

In this example, we will create an "About Us" page and add it to the sitemap.

Getting ready

We are continuing the practice of putting our code in /config/site_post.php for the purposes of demonstration and testing. In real-world use, your dynamic page creation would happen in controller files or in add-on packages.

We will be assigning the new page a page type with the handle of right_sidebar. If this doesn't exist in your own concrete5 installation, please adapt the recipe to suit your needs.

How to do it...

The steps for creating a page are as follows:

  1. Open /config/site_post.php in your code editor.
  2. Load the page type that the new page will use. We will load the page type using its handle (right_sidebar, in this case).
    $pageType = CollectionType::getByHandle('right_sidebar');
  3. Create an associative array that contains the fields that you wish to specify for the new page. In this example, we will only specify the page's name and handle.
    $data = array(
      'cName' => 'About Us',
      'cHandle' => 'about'
    );
  4. Load the parent page that the new page will be placed under. In this case, we are loading the home page, since the new page will be available at the top level.
    $parent = Page::getByID(1);
  5. Add the page by passing the $pageType object and $data array to the add function of the parent Page class.
    $newPage = $parent->add($pageType, $data);
  6. Output a message and exit the process.
    echo 'done!';
    exit;
  7. Visit your site's homepage to execute the code in site_post.php.
  8. If you see the success message, comment out the code in site_post.php so you can visit the newly created page. Otherwise, concrete5 will create a new page every time the site is loaded into a browser.
  9. Visit the newly created page at http://example.com/about.

How it works...

The add() function is a wrapper for the Collection class's add() function. Calling it will create a new Collection record in the database, and a new pending CollectionVersion record (which will be approved if the cvIsApproved variable is set to true or left blank). Then the new page will inherit the necessary permissions and get all of the blocks and other attributes from the Master Collection of the related page type.

There's more...

You will need to provide two parameters to the add() function, a CollectionType object (which we worked with earlier in this chapter), and an associative array containing all of the data needed to create the page. The associative array can contain the following fields.

See also

  • The Updating an existing page recipe
  • The Getting a page type by its ID recipe
  • The Getting a page type by its handle recipe
主站蜘蛛池模板: 沾益县| 泗水县| 南岸区| 苍溪县| 吐鲁番市| 浦城县| 望江县| 广灵县| 龙山县| 宝应县| 台州市| 通州区| 旅游| 安庆市| 镇江市| 淮安市| 永福县| 台北县| 仁布县| 安达市| 砚山县| 尼木县| 江口县| 合阳县| 格尔木市| 德兴市| 彩票| 东乡族自治县| 福安市| 连城县| 隆德县| 东安县| 盐池县| 馆陶县| 巴东县| 化隆| 宽城| 唐河县| 额敏县| 嘉祥县| 江山市|