netzmacht/contao-page-context 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

netzmacht/contao-page-context

Composer 安装命令:

composer require netzmacht/contao-page-context

包简介

This library provides tools to bootstrap the contao page contexts for custom routes.

README 文档

README

Build Status Version License Downloads Contao Community Alliance coding standard

This Contao extension allows developer manually boot the page context of Contao when using custom routes.

Parts of the Contao CMS relies on the existing of global state initialized when rendering a page. Modules, content elements, insert tags and custom extensions might rely that this state is initialized.

For instance Contao developers access the $GLOBALS['objPage'] as there is no other way to get the current page.

However, when using a custom entrypoint, for example for an API, you don't have this state initialized. Using the Contao library and functionality might end in unexpected errors.

This is the point there this extension steps in. It allows you to boot the page context manually for your route.

In detail it, initialize following steps:

  • The Contao framework
  • Constant BE_USER_LOGGED_IN and FE_USER_LOGGED_IN to false if not defined.
  • Loading the page from the database
  • Globals objPage, TL_ADMIN_NAME, TL_ADMIN_EMAIL, TL_KEYWORDS, TL_LANGUAGE
  • Initialize the locale of the request and the translator
  • Loads the default language file
  • Calls Controller::initializeStaticUrls()
  • Initializes the page layout (triggers getPageLayout hook)

Requirements

  • Contao ^4.13 || ^5.3
  • PHP ^8.1

Installation

php composer.phar require netzmacht/contao-page-context --update-no-dev -o 

Usage

1. Implement a PageIdDeterminator and register it properly

First you have to provide a PageIdDeterminator. It's responsible to extract the page id from the given request.

You should limit the determinator to your special use case, that's why there is the match() method.

<?php

declare(strict_types=1);

namespace My\Bundle;

use Netzmacht\Contao\PageContext\Request\PageIdDeterminator;
use Netzmacht\Contao\PageContext\Exception\DeterminePageIdFailed;
use Symfony\Component\HttpFoundation\Request;

final class MyPageIdDeterminator implements PageIdDeterminator
{
    public function match(Request $request): bool
    {
        return ($request->attributes->get('_my_context') === 'page');
    }

    public function determinate(Request $request): int
    {
        if (!$request->attributes->has('pageId')) {
            throw new DeterminePageIdFailed('Could not determine page id for from request.');
        }

        return $request->attributes->getInt('pageId');
    }
}

Now you have to register it as a service and tag it as Netzmacht\Contao\PageContext\Request\PageIdDeterminator.

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="My\Bundle\MyPageIdDeterminator">
            <tag name="Netzmacht\Contao\PageContext\Request\PageIdDeterminator" />
        </service>
    </services>
</container>

2. Prepare your route

As already seen in the example above, a custom route attribute is accessed, here _my_context. You should define it in your route configuration:

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        https://symfony.com/schema/routing/routing-1.0.xsd">

    <route id="my_route" controller="My\Bundle\Action\ApiAction" path="/my/api/{pageId}" >
        <default key="_my_context">page</default>
        <default key="_scope">frontend</default>
        <requirement key="pageId">\d+</requirement>
    </route>
</routes>

3. Accessing the page object

That's it. If you try to access $GLOBALS['objPage'] you should have the page object. Good news, you can avoid accessing the global state. Your current Request have a new attribute, called _page_context.

<?php

declare(strict_types=1);

namespace My\Bundle\Action;

use Netzmacht\Contao\PageContext\Request\PageContext;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class ApiAction
{
    public function __invoke(Request $request): Response
    {
        /** @var PageContext $context */
        $context = $request->attributes->get('_page_context');

        return new JsonResponse(
            [
                'pageId' => $context->page()->id,
                'rootId' => $context->rootPage()->id,
            ]
        );
    }
}

统计信息

  • 总下载量: 20.43k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2018-11-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固