PHP code example of netzmacht / contao-page-context
1. Go to this page and download the library: Download netzmacht/contao-page-context library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
netzmacht / contao-page-context example snippets
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');
}
}
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,
]
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.