We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Web api accept object parameter?

Hi everyone,

I've worked with ASP.NET before and in ASP.NET Web API, I could do something like this

public Product Add(Product item)
    {
        if (item == null)
        {
            throw new ArgumentNullException("item");
        }
        item.Id = _nextId++;
        products.Add(item);
        return item;
    }

I mean I can create a model class (Product) and put an instance of it in parameter

How can I do this in Phalcon Micro? Recently, with Micro, I can only put primitive type as parameter.

Thank you very much



7.9k
Accepted
answer

Hi you cant retieve PHP object from HTTP.

But you can create additional layer for that.

for example


call SomeController
{
    $private repository;

    public function __construct(SomeRepository $repo)
    {
        $this->repository = repository;
    }

    public function addAction($id = "")
    {
        $product = $this->repository->findByID($product);
        $this->repository->add($product)
    }
}