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

  • concrete5 Cookbook
  • David Strack
  • 513字
  • 2021-08-13 16:15:57

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
主站蜘蛛池模板: 文昌市| 城步| 共和县| 资中县| 自治县| 英超| 玛纳斯县| 正宁县| 海原县| 称多县| 安仁县| 遂溪县| 光山县| 蒙城县| 昌图县| 天水市| 化州市| 固安县| 大化| 平谷区| 定陶县| 西城区| 莒南县| 裕民县| 建瓯市| 墨脱县| 海城市| 文昌市| 巴青县| 门源| 岳西县| 莆田市| 漯河市| 望都县| 连州市| 甘泉县| 康平县| 浠水县| 钟山县| 赫章县| 榆林市|