Hallo Contao-Community,
ich programmiere gerade eine API. Per GET - Anfrage lässt sich die API ohne Probleme aufrufen und es lassen sich auch entsprechende Services (wie z.B. contao.framework) laden.
Bei POST-Anfragen erhalte ich allerdings ein 500er zurück. Die Routes definiere ich über Annotations.
Ich weiß momentan gerade nicht wodran es hapert. Vllt habt ihr ja eine Idee.
Ein Ausschnitt aus den Logs:
HTML-Code:
[2019-07-03 14:01:08] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2019-07-03 14:01:08] request.CRITICAL: Uncaught PHP Exception Error: "Call to a member function get() on null" at /shared/httpd/ls/src/Controller/Projects.php line 20 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function get() on null at /shared/httpd/ls/src/Controller/Projects.php:20)"} []
Ein kurzer Dump auf \System::getContainer() bei einer POST-Anfrage ergibt NULL. Muss ich den Container in diesem Fall anders laden?
Composer Autoloader, Composer Cache und Contao Cache habe ich über den Contao-Manager geleert.
Den Controller habe ich mit zwei identischen Testmethoden gebaut. Mein Controller dazu:
HTML-Code:
namespace App\Controller;
use App\Models\ProjectModel;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
class Projects extends AbstractController
{
...
/**
* @Route("/api/test/post", defaults={"_scope" = "frontend", "_token_check" = true}, methods={"POST"})
*
* @return JsonResponse
*/
public function test_post()
{
$contaoFramework = \System::getContainer()->get('contao.framework');
$contaoFramework->initialize();
\System::loadLanguageFile('default');
return new JsonResponse([
'success' => 'POST'
],200);
}
/**
* @Route("/api/test/get", defaults={"_scope" = "frontend", "_token_check" = true})
*
* @return JsonResponse
*/
public function test_get()
{
$contaoFramework = \System::getContainer()->get('contao.framework');
$contaoFramework->initialize();
\System::loadLanguageFile('default');
return new JsonResponse([
'success' => 'GET'
],200);
}
...
}
Meine composer.json:
HTML-Code:
{
"type": "project",
"require": {
"contao/calendar-bundle": "^4.7",
"contao/comments-bundle": "^4.7",
"contao/conflicts": "*@dev",
"contao/faq-bundle": "^4.7",
"contao/listing-bundle": "^4.7",
"contao/manager-bundle": "4.7.*",
"contao/news-bundle": "^4.7",
"contao/newsletter-bundle": "^4.7",
"derhaeuptling/contao-lazy-images": "^3.0",
"madeyourday/contao-rocksolid-custom-elements": "^2.2",
"madeyourday/contao-rocksolid-slider": "^2.0",
"terminal42/contao-changelanguage": "^3.1"
},
"extra": {
"contao-component-dir": "assets"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"scripts": {
"post-install-cmd": [
"Contao\\ManagerBundle\\Composer\\ScriptHandler::initializeApplication"
],
"post-update-cmd": [
"Contao\\ManagerBundle\\Composer\\ScriptHandler::initializeApplication"
]
}
}
Braucht ihr noch weitere Informationen von mir?
Danke schonmal für Eure Hilfe
Lesezeichen