Giter VIP home page Giter VIP logo

Comments (42)

tgoeminne avatar tgoeminne commented on May 23, 2024 2

Feels like the plugin is designed to give people problems doing something so simple. It is really terrible piece of code.

from cakephp-api.

steinkel avatar steinkel commented on May 23, 2024 2

@tgoeminne this plugin has some defaults and conventions that may not match what you usually do or expect in your project. I've used it many times, and it provides what I usually need when dealing with an API project. I understand you could be frustrated because it does not work as you expect though.

What contributions could you do to improve the docs or the code so other users could benefit from them?

I see @skie is already taking some time to help you... maybe this could become an input for the documentation later for other users trying to do the same you're doing here?

Thanks,

from cakephp-api.

skie avatar skie commented on May 23, 2024 1

Api plugin is 100% RESTful. So routes based on REST verbs and urls based on REST convetions. So correct there would be GET, /articles for index, and GET,/articles/:id for view, POST,/articles for add, and PUT,/articles/:id for edit, and finally DELETE,/aritcles/:id for delete.

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

`<?php
namespace App\Service\v1;

use CakeDC\Api\Service\Service as Service;

class ArticlesService extends Service {

protected $_actionsClassMap = [
    'describe' => '\CakeDC\Api\Service\Action\CrudDescribeAction',
    'index' => '\App\Service\v1\Action\Articles\IndexAction',
    'view' => '\App\Service\v1\Action\Articles\ViewAction',
    'add' => '\App\Service\v1\Action\Articles\AddAction',
    'edit' => '\App\Service\v1\Action\Articles\EditAction',
    'delete' => '\App\Service\v1\Action\Articles\DeleteAction',
];

}`

Then

`<?php
namespace App\Service\v1\Action\Articles;

use CakeDC\Api\Service\Action\CrudIndexAction;

class IndexAction extends CrudIndexAction {

public function execute()
{
    $results = $this->_getEntities();
    
    return $results;
}

}`

{"status":"error","message":"A route matching "/articles" could not be found.","code":404,"data":null}

from cakephp-api.

skie avatar skie commented on May 23, 2024

Is versions enabled in config/api.php?

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

yes

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

'useVersioning' => true,
'versionPrefix' => 'v',

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

When I use FallbackService instead of service I can access /api/v1/articles and then it works, but then also the actions view,... does not work (missing routes). When I use Service, I can not even access /api/v1/articles (missing route). In my other post I also mention the config, I have to put vv1 in the config files so that the version config is loaded when request v1 on fallbackservice...

from cakephp-api.

skie avatar skie commented on May 23, 2024

i would create class ArticlesService extends FallbackService and overload only needed actions. Also I recommend to take look on https://github.com/gothinkster/cakephp-realworld-example-app

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

I just use actionsClassMap and then use execute method in the action. Is that not enough to get it to work?

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

Everything just show missing routes when I use api/articles/view/1, if I use versioning or not, this plugin another type of give me a serious headache to try it to get to work.... also the documentation seriously lacking as usual. Did you see the link to routes is pointing to 404?

from cakephp-api.

skie avatar skie commented on May 23, 2024

you used Service not CrudService or FallbackService

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

with fallbackservice is the same, it doesn't work.

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

I am looking at it now. I thought it would be more easy to have the request go to /api/articles/view/18 then to api/articles/18

Need to look into it more, but is pretty confusing. Is there somewhere code or like an sdk that an end user would use on third party php website to access the api?

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024
$actions = [
            'index' => $reverseRouter->link('self', $path, 'GET'),
            'add' => $reverseRouter->link('add', $path, 'POST'),
            'edit' => $reverseRouter->link('edit', $path . '/{id}', 'PUT'),
            'delete' => $reverseRouter->link('delete', $path . '/{id}', 'DELETE'),
        ];

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

I also see that you can use cakdc users to authorize, but I can not find any documentation on how to do this. Both for sending the request and setting it up.

I am sending post request and I get csrf token missing...

There is a link to jwt tutorial which also goes to a 404 page...

from cakephp-api.

delamux avatar delamux commented on May 23, 2024

I also see that you can use cakdc users to authorize, but I can not find any documentation on how to do this. Both for sending the request and setting it up.

I am sending post request and I get csrf token missing...

There is a link to jwt tutorial which also goes to a 404 page...

Sorry, meaby the link is broken on the main documentation.

https://github.com/CakeDC/cakephp-api/blob/master/docs/Documentation/Auth/jwt.md

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

Is there somewhere a tutorial how to do the api request with auth via cakedc users? And also so it works with csrf and such...?

from cakephp-api.

delamux avatar delamux commented on May 23, 2024

Hello @tgoeminne, I don't know if this repo can Help you. I did it last year.

https://github.com/delamux/apiplug

from cakephp-api.

skie avatar skie commented on May 23, 2024

https://www.cakedc.com/yevgeny_tomenko/2020/06/29/cakedc-api-plugin-authentication-and-authorization

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

I am using basic token authorization for now. where you add ?token=xxxxx to the back

I am running in the problem with request POST being blackholed due to missing csrf token. How can you disable csrf token in service/action?

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

Then disable the csrf middleware by changing routes and application.php. Now the POST request being blackholed.

from cakephp-api.

steinkel avatar steinkel commented on May 23, 2024

is SecurityComponent enabled for these actions?

The way to disable Csrf is creating a specific routing scope "without" loading the csrf middleware into it.

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

Yes I disabled csrf, now looking how I can dissable security component for specific action. I thought the point of mapCors was that it is allowed by default from another domain, but it seems it isn't working like that.

I try put this in Articleservice but not work
`
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);

    $this->Security->setConfig('unlockedActions', ['add']);
}

`

from cakephp-api.

steinkel avatar steinkel commented on May 23, 2024

That should not be an issue if you plug Api using the https://github.com/CakeDC/cakephp-api/blob/master/src/Middleware/ApiMiddleware.php

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

How do I do that?

from cakephp-api.

steinkel avatar steinkel commented on May 23, 2024

https://github.com/gothinkster/cakephp-realworld-example-app/blob/master/src/Application.php#L102

Example there ^

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

Where do I insert that then? There is nowhere any information about this in the docs or configuration/installation... Why is this so complicated to get it to work properly?

from cakephp-api.

steinkel avatar steinkel commented on May 23, 2024

We need to properly document the Api middleware load, you're right.

I understand the specific problem is Security component interacting with your ApiController, so I would suggest either:

  • Setup ApiMiddleware to ignore the controller layer and bypass Security
  • Properly configure Security, for example in your AppController, check if the request belongs to the Api and disable SecurityComponent.

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

if(!$this->getRequest()->getAttributes()['params']['controller'] == 'Api'){ $this->loadComponent('Security'); }

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

Now when I post I receive another error:

{"error":{"code":422,"message":"Validation failed","validation":{"version":{"_required":"This field is required"}}}}

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

I have set useVersioning to false, then why is this field required?

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

I don't understand why Api is considered a "controller", when it should really just be a "prefix"...

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

Ok I got it to work now after disabling both csrf and security for api controller.

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

I would have probably been better of just making a prefix api and then send response with json....

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

I have a question about the post data adding of files. What is the best way to send files to api? I am using cakephp upload...

from cakephp-api.

steinkel avatar steinkel commented on May 23, 2024

Hi, posting a file is the same as posting any other data, the tmp file will be available in the request and using upload or any other plugin to manage the posted data will process the file and move it somewhere else in your backend. Are you facing any specific issue while dealing the the uploaded files?

Thanks,

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

Still need to test it..

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

Hi,

I am sending postwithmultipartformdata to api and then when I do print_r on $this->getData() to debug log. I can see

[--------------------------d1f95fef5s642baf

Content-Disposition:_attachment;_name] => "test.json"; filename="test.json"
Content-Type: application/octet-stream

and then json contents.

If I understand correctly in this case $this->getData() is a file? Then how do I get the actual json data in an array?

from cakephp-api.

skie avatar skie commented on May 23, 2024

Possible you want to define custom RequestParser inherited from Api\Service\RequestParser\BaseParser or just want access to request object with $this->getService()->getRequest()

from cakephp-api.

tgoeminne avatar tgoeminne commented on May 23, 2024

I do getData but I see content disposition inthere which is normally a header?

from cakephp-api.

skie avatar skie commented on May 23, 2024

closed as obsolete support issue

from cakephp-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.