Giter VIP home page Giter VIP logo

Comments (5)

mmghv avatar mmghv commented on August 22, 2024 1

I did some edits to make it work,
Undo the changes and update to the latest version using composer update and follow these instructions : DingoAPI Integration

Let me know if everything is working good.

from lumen-route-binding.

mmghv avatar mmghv commented on August 22, 2024

Hi,
I reviewed Dingo code and found that they re-bound the FastRoute dispatcher that's why LumenRouteBinding dispatcher is not being called ..

To make it work it will need some workaround,
You will need to extend this Dingo class Dingo\Api\Routing\Adapter\Lumen and create the following class & put it somewhere, maybe in app/Custom :

// app/Custom/DingoAdapter.php

namespace App\Custom;

use Illuminate\Http\Request;
use Dingo\Api\Exception\UnknownVersionException;
use Dingo\Api\Routing\Adapter\Lumen as BaseDingoAdapter;

class DingoAdaptre extends BaseDingoAdapter
{
    /**
     * The dispatcher resolver callable.
     *
     * @var callable
     */
    protected $dispatcherResolver;

    /**
     * Set callable to get dispatcher instance.
     *
     * @param callable $dispatcherResolver
     */
    public function setDispatcherResolver(callable $dispatcherResolver) {
        $this->dispatcherResolver = $dispatcherResolver;
    }

    /**
     * Dispatch a request.
     *
     * @param \Illuminate\Http\Request $request
     * @param string                   $version
     *
     * @return mixed
     */
    public function dispatch(Request $request, $version)
    {
        if (! isset($this->routes[$version])) {
            throw new UnknownVersionException;
        }

        $this->removeMiddlewareFromApp();

        $routes = $this->routes[$version];

        $this->app->setDispatcher(
            call_user_func($this->dispatcherResolver, $routes->getData())
        );

        $this->normalizeRequestUri($request);

        return $this->app->dispatch($request);
    }
}

Then replace app/Providers/RouteBindingServiceProvider.php with the following :

// app/Providers/RouteBindingServiceProvider.php

namespace App\Providers;

use App\Custom\DingoAdaptre;
use mmghv\LumenRouteBinding\BindingResolver;
use mmghv\LumenRouteBinding\FastRouteDispatcher;
use FastRoute\RouteParser\Std as StdRouteParser;
use FastRoute\DataGenerator\GroupCountBased as GcbDataGenerator;
use mmghv\LumenRouteBinding\RouteBindingServiceProvider as BaseServiceProvider;

class RouteBindingServiceProvider extends BaseServiceProvider
{
    /**
     * Boot the service provider
     */
    public function boot()
    {
        // The binder instance
        $binder = $this->binder;

        // Here we define our bindings
    }

    /**
     * Register the service provider.
     */
    public function register()
    {
        // Register our binding resolver in the service container
        $this->binder = new BindingResolver([$this->app, 'make']);
        $this->app->instance('bindingResolver', $this->binder);

        // replace dingo adapter which was set by dingo to allow for route binding,
        // this provider needs to be registered after dingo is registered
        $this->setDingoAdapter();
    }

    /**
     * Replace dingo adapter in the container.
     */
    protected function setDingoAdapter()
    {
        $this->app->singleton('api.router.adapter', function ($app) {
            $adapter = new DingoAdaptre($app, new StdRouteParser, new GcbDataGenerator, '');

            $adapter->setDispatcherResolver(function($data) {
                return $this->getDispatcher($data);
            });

            return $adapter;
        });
    }

    /**
     * Get LumenRoutebinding dispatcher instance
     *
     * @param  array $data
     *
     * @return FastRouteDispatcher
     */
    public function getDispatcher($data)
    {
        // Create a new FastRoute dispatcher (our extended one)
        $dispatcher = new FastRouteDispatcher(null);
        $dispatcher->setBindingResolver($this->binder);
        $dispatcher->setDispatcherResolver(function($dispatcher) use ($data) {
            return new $dispatcher($data);
        });

        return $dispatcher;
    }
}

IMPORTANT Make sure to register App\Providers\RouteBindingServiceProvider after Dingo provider NOT before it.

Please let me know if it worked ..

from lumen-route-binding.

ShawnRong avatar ShawnRong commented on August 22, 2024

Thanks for your reply.I put the same code in my project.And I did register RouteBindingServiceProvider after Dingo provider.It's still not work.Maybe I should go back to use Laravel.

from lumen-route-binding.

ShawnRong avatar ShawnRong commented on August 22, 2024

That's cool.It works now!Thank you so much!

from lumen-route-binding.

mmghv avatar mmghv commented on August 22, 2024

You're welcome, glad to help :)

from lumen-route-binding.

Related Issues (13)

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.