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

_remap is not working #2277

Closed
nowackipawel opened this issue Sep 27, 2019 · 5 comments · Fixed by #2300
Closed

_remap is not working #2277

nowackipawel opened this issue Sep 27, 2019 · 5 comments · Fixed by #2300

Comments

@nowackipawel
Copy link
Contributor

ReflectionException

Method App\Controllers\Api\V1::timestamp() does not exist

or:

Error

Call to undefined method App\Controllers\Api\V1::get_timestamp() 

occurs if some of two methods (one according to Routes and one according to _remap code is not available)

and the simpliefied code:

<?php namespace App\Controllers\Api;

use CodeIgniter\Controller;

class V1 extends Controller
{
    public function _remap($method, ...$params)
    {
        return $this->{'get_' . $method}();
    }

    public function get_timestamp()
    {
        return __METHOD__;
    }

    public function timestamp()
    {
        return __METHOD__;
    }
}

Anyway if two methods are exists only that one which its name is calculated in _remap returns something to the screen.
App\Controllers\Api\V1::get_timestamp

@jim-parry
Copy link
Contributor

This doesn't sound like a bug.
Your remap will attempt get_get_timestamp() after remapping, giving the undefined method you show above, with the name before remapping.
Further your remapping ensures that timestamp() on its own will not be called.
The observed behavior is just what I would expect to happen.

@lonnieezell
Copy link
Member

From the docs:

If your controller contains a method named _remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which method is called, allowing you to define your own method routing rules.

So, yes, whatever is calculated in the remap method is the only thing that will be called.

@nowackipawel
Copy link
Contributor Author

@lonnieezell I reckon I did not write something clear.
The problem is when in this class there is no method called "timestamp" I've got

ReflectionException

Method App\Controllers\Api\V1::timestamp() does not exist

I expect that only get_timestamp() calculated by _remap() will be called and it is, but ReflectionClass is also checking if timestamp() is callable and caused exception.

@lonnieezell
Copy link
Member

You could have saved us some time if you would have reported the file the error was happening from - it's the Routes Collector for the debug toolbar. And there is an issue with the toolbar, but not with the actual routing. Working on a fix.

@lonnieezell lonnieezell reopened this Oct 4, 2019
lonnieezell added a commit that referenced this issue Oct 4, 2019
@nowackipawel
Copy link
Contributor Author

@lonnieezell : I would love too but this issue was closed :/. I guess you've already checked it , but if not I will be happy to restore that file and upload screenshots with debug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants