Luracast / Restler

Simple and effective multi-format Web API Server to host your PHP API as Pragmatic REST and/or RESTful API
http://luracast.com/products/restler/
GNU Lesser General Public License v2.1
1.36k stars 315 forks source link

Need help with a namespace change #694

Closed celdaran closed 8 months ago

celdaran commented 8 months ago

I tested Restler using the documented gateway, but using a namespaced class:

<?php

require_once './restler.php';

use Luracast\Restler\Restler;

$r = new Restler();
$r->addAPIClass('App\Service\Api\Say');
$r->handle();

File on disk in ./src/Service/Api/Say.php:

<?php namespace App\Service\Api;

class Say
{
    /**
     * @param string $to
     * @return array
     */
    function hello(string $to = 'world') {
        return [
            'result' => "Hello $to!",
            'to' => $to,
        ];
    }

    /**
     * @param string $to
     * @return array
     */
    function hi($to) {
        return [
            'result' => "Hi $to!",
            'to' => $to,
        ];
    }
}

No problems! Worked perfectly. But I wanted to change the namespace from App\Service\Api\Say to App\Api\Say and then move the physical PHP class accordingly. However, when I do that, the class is suddenly invisible. Like, the autoloader can't find it. This is my restler.php file:

<?php
use Luracast\Restler\Defaults;
use Luracast\Restler\Format\HtmlFormat;

if (is_readable(__DIR__ . '/vendor/autoload.php')) {
    //if composer auto loader is found use it
    require __DIR__ . '/vendor/autoload.php';
    class_alias('Luracast\\Restler\\Restler', 'Restler');
} else {
    //otherwise use the restler auto loader
    require_once __DIR__.'/src/Luracast/Restler/AutoLoader.php';
    return call_user_func(function () {
        $loader = Luracast\Restler\AutoLoader::instance();
        spl_autoload_register($loader);
        return $loader;
    });
}
Defaults::$cacheDirectory = __DIR__ . '/cache';
HtmlFormat::$viewPath = __DIR__ . '/views';

Been at this for an hour and just out of ideas. Any thoughts?

celdaran commented 8 months ago

And of course I figure it out right after posting. I forgot to update my autoload section in composer.json

:eyeroll: