Hallo,
ich habe versucht ein einfaches App-Bundle zu erstellen, welches eine eigene Symfony-Route verwendet.
So siehts aus:
app/ContaoManagerPlugin.php
Code:
<?php
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
use Contao\ManagerPlugin\Routing\RoutingPluginInterface;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class ContaoManagerPlugin implements BundlePluginInterface, RoutingPluginInterface
{
/**
* {@inheritdoc}
*/
public function getBundles(ParserInterface $parser)
{
return [
BundleConfig::create(AppBundle\AppBundle::class)
->setLoadAfter([ContaoCoreBundle::class])
];
}
/**
* Returns a collection of routes for this bundle.
*
* @param LoaderResolverInterface $resolver
* @param KernelInterface $kernel
*
* @return RouteCollection|null
*
* @throws \Exception
*/
public function getRouteCollection(LoaderResolverInterface $resolver, KernelInterface $kernel)
{
return $resolver
->resolve(__DIR__.'/../src/AppBundle/Resources/config/routing.yml')
->load(__DIR__.'/../src/AppBundle/Resources/config/routing.yml');
}
}
src/AppBundle/AppBundle.php
Code:
<?php
namespace AppBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppBundle extends Bundle
{
}
src/AppBundle/Controller/TestX.php
Code:
<?php
namespace AppBundle\Controller;
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\FilesModel;
use Contao\StringUtil;
use Contao\Validator;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
class TestX extends Controller
{
protected $rootDir;
protected $session;
protected $framework;
public function __construct(string $rootDir, Session $session, ContaoFramework $framework)
{
$this->rootDir = $rootDir;
$this->session = $session;
$this->framework = $framework;
}
public function loadAction($uuid): Response
{
$this->framework->initialize();
if (!Validator::isBinaryUuid($uuid)) {
$uuid = StringUtil::uuidToBin($uuid);
}
$filesModel = FilesModel::findByUuid($uuid);
if (null === $filesModel || !\is_file($this->rootDir . '/' . $filesModel->path)) {
throw new PageNotFoundException('File not found [' . $filesModel->path . ']');
}
$this->session->save();
return $this->json(['path' => $filesModel->path]);
}
}
src/AppBundle/Resources/config/routing.yml
Code:
testx:
path: /testx/{uuid}
defaults:
_controller: 'AppBundle\Controller\TestX'
_scope: frontend
_token_check: true
requirements:
uuid: '[a-fA-F0-9]{32}'
composer.json
Code:
"autoload": {
"psr-4": { "AppBundle\\": "src/AppBundle/" },
"classmap": ["app/ContaoManagerPlugin.php"]
}
Wenn ich das ganze mit einem Aufruf wie zb.:
http://myinstallation/testx/051878bc...2-40663e0458b1
... teste, bekomme ich nur einen "404 Not Found".
Eigentlich sollte der Pfad der Bild-Datei zurückgegeben werden.
Wisst ihr vielleicht was ich da falsch mache?
Danke!
Lesezeichen