Creating a controller that handles POST

Discussion forum for Extension Writers regarding Extension Development.
tig_
Registered User
Posts: 23
Joined: Fri Aug 23, 2024 2:42 pm

Creating a controller that handles POST

Post by tig_ »

I have:

routing.yml

Code: Select all

tig_blobuploader_blobuploader:
    path: /blobuploader
    defaults: { _controller: tig.blobuploader.controller.blobuploader::handle_request }
    methods: [POST]
services.yml

Code: Select all

services:
...
    tig.blobuploader.controller.blobuploader:
        class: tig\blobuploader\controller\blobuploader
        arguments:
            - '@user'
            - '@request'
            - '%core.root_path%'
            - '%core.php_ext%'
blobuploader.php

Code: Select all

<?php

namespace tig\blobuploader\controller;

use Symfony\Component\HttpFoundation\Response;

class blobuploader
{
    protected $user;
    protected $request;
    protected $root_path;
    protected $php_ext;

    public function __construct(
        user $user,
        request $request,
        $root_path,
        $php_ext
    ) {
        $this->user = $user;
        $this->request = $request;
        $this->root_path = $root_path;
        $this->php_ext = $php_ext;
    }

    public function handle_request ()
    {
        ...
     }
When I do curl -X POST -F "image=@images/spacer.gif" http://localhost/blobuploader

I get

Code: Select all

<br />
<b>Fatal error</b>:  Uncaught TypeError: Symfony\Component\HttpKernel\Event\FilterControllerEvent::__construct(): Argument #2 ($controller) must be of type callable, array given, called in /var/www/html/phpbb/vendor/symfony/http-kernel/HttpKernel.php on line 138 and defined in /var/www/html/phpbb/vendor/symfony/http-kernel/Event/FilterControllerEvent.php:32
Stack trace:
#0 /var/www/html/phpbb/vendor/symfony/http-kernel/HttpKernel.php(138): Symfony\Component\HttpKernel\Event\FilterControllerEvent-&gt;__construct()
#1 /var/www/html/phpbb/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw()
#2 /var/www/html/phpbb/app.php(35): Symfony\Component\HttpKernel\HttpKernel-&gt;handle()
#3 {main}
  thrown in <b>/var/www/html/phpbb/vendor/symfony/http-kernel/Event/FilterControllerEvent.php</b> on line <b>32</b><br />
  
I've searched and searched for examples of POST controllers and haven't found one.

Help, please!
tig_
Registered User
Posts: 23
Joined: Fri Aug 23, 2024 2:42 pm

Re: Creating a controller that handles POST

Post by tig_ »

Bump.
User avatar
Steve
Registered User
Posts: 1598
Joined: Tue Apr 07, 2009 7:48 pm
Location: Co. Durham, England
Name: Steven Clark

Re: Creating a controller that handles POST

Post by Steve »

https://area51.phpbb.com/docs/dev/3.3.x ... -injection
No files requested, your __construct( is wrongly set.

Code: Select all

class blobuploader
{
    protected $user;
    protected $request;
    protected $root_path;
    protected $php_ext;

    public function __construct(
        \phpbb\user user $user,
        \phpbb\request\request $request,
        $root_path,
        $php_ext) 
	{
        $this->user = $user;
        $this->request = $request;
        $this->root_path = $root_path;
        $this->php_ext = $php_ext;
    }

    public function handle_request()
    {
        //...
        return $this;
    }
@ The Chief Medical Officers guideline for men is that: You are safest not to drink regularly more than 14 units per week.
- I drank that today++ :lol: đŸș
tig_
Registered User
Posts: 23
Joined: Fri Aug 23, 2024 2:42 pm

Re: Creating a controller that handles POST

Post by tig_ »

Nevermind (maybe):

My routing.xml had a typo (note two ::).

Code: Select all

tig_blobuploader_controller:
    path: /uploader/{name}
    defaults: { _controller: tig.blobuploader.controller.main:handle }
    
tig_blobuploader_blobuploader:
    path: /blobuploader
    defaults: { _controller: tig.blobuploader.controller.blobuploader::handle_request }
    methods: [POST]

Return to “Extension Writers Discussion”