Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Method class not found #568

Closed
spacebiscuit opened this issue Jan 15, 2018 · 22 comments
Closed

Custom Method class not found #568

spacebiscuit opened this issue Jan 15, 2018 · 22 comments

Comments

@spacebiscuit
Copy link

I'm trying to add a custom action, following these instructions

http://crud.readthedocs.io/en/latest/actions/custom.html#using-custom-named-controller-actions

I have the following:

public function initialize() {
        
        parent::initialize();
        
        $this->loadComponent('Crud.Crud', [
            'actions' => [
                'test' => ['className' => 'App\Controller\test']
            ],
            'listeners' => ['Crud.Api'],             
        ],'RequestHandler'
        );
        
        $this->Crud->config(['listeners.api.exceptionRenderer' => 'App\Error\ExceptionRenderer']);
        $this->Crud->addListener('relatedModels', 'Crud.RelatedModels');

    }
    
    public function beforeFilter(Event $event){
        parent::beforeFilter($event);
        $this->Crud->addListener('Crud.Api');
    }

    public function test() {         
        $this->Crud->execute();
    }

When I call test.json I get the following:

message: "Class 'App\Controller\TestAction' not found",

Also are the documents correct, it says to use: \App\Crud\Action\ but the top of my controller use the namespace: namespace App\Controller. I tried both but the result is the same - missing class.

What am I doing wrong?

@ADmad
Copy link
Member

ADmad commented Jan 15, 2018

The action class must be under namespace as documented.

@ADmad ADmad closed this as completed Jan 15, 2018
@spacebiscuit
Copy link
Author

spacebiscuit commented Jan 15, 2018

I am using the namespace as documnted:

\App\Crud\Action\

The controller is in:

src/Controllers. Is this wrong. I've done as much as the document says but the action isn't called.

@bravo-kernel
Copy link
Contributor

@spacebiscuit set that/or the correct namespace here instead of:

    'test' => ['className' => 'App\Controller\test']

@spacebiscuit
Copy link
Author

@bravo-kernel I have that in my code - see my gist

https://gist.github.com/spacebiscuit/84cebc423c66f05c1705b9049172224a

I still get missing class 'test'

@ADmad
Copy link
Member

ADmad commented Jan 15, 2018

Use the proper class name with proper casing. It should be \Test not \test.

@spacebiscuit
Copy link
Author

I've tried with:

'test' => ['className' => '\App\Controller\Test']
"message": "Class \u0027\App\Controller\Test\u0027 not found",

'test' => ['className' => '\App\Controller\TestAction']
message": "Class \u0027\App\Controller\TestAction\u0027 not found",

'test' => ['className' => '\App\Crud\Action\TestAction']
"message": "Class \u0027\App\Crud\Action\TestAction\u0027 not found",

@bravo-kernel
Copy link
Contributor

How about 🎲 and try:

'test' => ['className' => '\App\Crud\Action\Test']

@spacebiscuit
Copy link
Author

spacebiscuit commented Jan 15, 2018

I'm not sure why I tried, but I did and the result s the same:

'test' => ['className' => '\App\Crud\Action\Test']
{message: "Class '\App\Crud\Action\Test' not found"}

Is anyone able to test this to confirm the feature does actually work?

@josegonzalez
Copy link
Member

With that last example, can you screenshot the exact filepath and gist the class you are using?

@spacebiscuit
Copy link
Author

Hi @josegonzalez as requested:

File Path:

file-path

The class:

https://gist.github.com/spacebiscuit/a1deaa477914e68ab731dfd776772690

I am calling: offers/test.json

@josegonzalez
Copy link
Member

Where is your TestAction class? I don't see that in your screenshot, and you gisted the controller, not the action class.

What exactly are you trying to do?

@spacebiscuit
Copy link
Author

@josegonzalez

I was following this example which calls the 'drinkbeer' function in the Users controller.

http://crud.readthedocs.io/en/latest/actions/custom.html#using-custom-named-controller-actions

To give an overview of what I am trying to achieve. I have a Messages model, when I save to Messages via the API I want to fire an email to the User to notify them of a Message. I don't know if I should do:

  1. Send an email in the afterSave of Message.add

OR

  1. After Message.add make another Call to Email.add (or similar).

Email.add was what I was trying to test with the 'test' call in my example above. It seems cleaner to keep the Message.add and Email.add separate so that the two actions are distinct, or perhaps I am over-complicating matter and should keep them in the same action? Since Email.add wouldn't be related to a Model per se I was trying to add my custom function to handle the sending of emails via the API.

Any suggestion and advice regarding the 'correct' approach would be welcome - thnaks!

@josegonzalez
Copy link
Member

Okay, do you have a TestAction class?

@spacebiscuit
Copy link
Author

No i dont have that as the tutorial doesnt have the drinkbeer class but a method instead. Im confused as to whether i need a 'test' class or method.

@josegonzalez
Copy link
Member

josegonzalez commented Jan 16, 2018

The tutorial mentions the following:

create your own custom Action.

It assumes you created an action class at that path. You'll need to do similar, otherwise what exactly are you expecting that to execute?

Note: the custom action class should be in the namespace that you provide when initializing the controller.

@spacebiscuit
Copy link
Author

I don't see how my example differs from the example in the book

https://crud.readthedocs.io/en/5.2.2/actions/custom.html#using-custom-named-controller-actions.

The example is using: UsersController::drinkbeer()
In my case it is: OffersController::test()

The example has 'drinkbeer' action. I have 'test' action.

The example uses the namepsace: '\App\Crud\Action\DrinkbeerAction' but it is not clear how this relates to the UsersController::drinkbeer.

Can I not just call my own 'test' function in the Listings controller? I know this is painful, it is for me too, I've been trying to get this to work for 2 days now.

@josegonzalez
Copy link
Member

You need to have a class called TestActionin src/Crud/Action/TestAction.php. The example there omits that class and presupposes you have that class available in src/Crud/Action/DrinkBeerAction.php.

Since you don't have the action class, the class isn't found, thus your error.

@josegonzalez
Copy link
Member

josegonzalez commented Jan 16, 2018

Example TestAction class (modified from the example at the top of that page):

<?php
namespace App\Crud\Action;

class TestAction extends \Crud\Action\BaseAction
{
    /**
     * Default settings
     *
     * @var array
     */
    protected $_defaultConfig = [
        'enabled' => true,
        'scope' => 'table',
        'findMethod' => 'all',
        'view' => null,
        'viewVar' => null,
        'serialize' => [],
        'api' => [
            'success' => [
                'code' => 200
            ],
            'error' => [
                'code' => 400
            ]
        ]
    ];

    /**
    * Generic handler for all HTTP verbs
    *
    * @return void
    */
    protected function _handle()
    {
        $query = $this->_table()->find($this->findMethod());
        $items = $this->_controller()->paginate($query);
    }
}

@josegonzalez
Copy link
Member

I'll reopen this issue until we've figured out what the next steps are here. It's pretty clear the docs are... subpar at best here. Plus, a DrinkBeerAction makes no sense.

@josegonzalez josegonzalez reopened this Jan 16, 2018
@spacebiscuit
Copy link
Author

Ok so in src/Action I cresated TestAction.php. The file is as you have supplied above.

<?php
namespace App\Crud\Action;

class TestAction extends \Crud\Action\BaseAction
....

Then in my offers controller:

'test' => ['className' => '\App\Crud\Action\TestAction']

Still getting App\Crud\Action\TestAction not found though.

Just to confirm, I have to create the my own class if I want a custom action? I can't use my existing Offers controller?

@josegonzalez
Copy link
Member

josegonzalez commented Jan 16, 2018

Can you go on our slack channel? I feel as though you're not quite understanding what the difference between a Crud Action class and a CakePHP controller is. We can figure out what the gap is and then fill in the docs once we do. My nick there is savant.

@juvenalmuniz
Copy link

I was having the same problem of class not found. I've solved it by connecting an additional resource method in the routes.php file.

   $routes->resources('Cocktails', [
     'map' => ['beer' => ['action' => 'beer', 'method' => 'GET']]
   ]);

'beer' is the custom action.

@jippi jippi closed this as completed Dec 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants