Home > Blockchain >  How to get Class Method on Laravel
How to get Class Method on Laravel

Time:02-15

Hello I try to get all Methods on a Class Controller on Laravel but I was failed. I use get_class_methods function to get class methods.

Here is my ConfigController codes:

use Modules\Order\Http\Controllers\V1\Web\OrderController;
class ConfigController extends Controller
{
    public function index(){
        $methods = get_class_methods(new OrderController::class);
        dd($methods);
    }
}

After executing the code the result is:

Class "Modules\Order\Http\Controllers\V1\Web\OrderController" not found

And I'm sure that that class exists on that namespace because I have already called it on route.

How to do that on the right way?

Thank you.

CodePudding user response:

Have you tried reflection yet? My example below:

$reflection = new ReflectionMethod($className, $methodName);
$parameter = $reflection->getParameters();
foreach ($parameter as $param) {
    echo $parameter->getName();
    echo $parameter->isOptional();
}

CodePudding user response:

This is the answer, I just miss s on the method name:

use ReflectionClass;
...
$reflection = new ReflectionClass(OrdersController);
dd($reflection);
...
  • Related