示例#1
0
 /**
  * Bootstrap any application services.
  *
  * @param Router $api
  */
 public function boot(Router $api)
 {
     $path = app_path('Api/routes.php');
     $api->version('v1', ['namespace' => $this->namespace], function ($api) use($path) {
         require $path;
     });
 }
示例#2
0
文件: Request.php 项目: riclt/api
 /**
  * Send the request through the Dingo router.
  *
  * @param \Dingo\Api\Http\Request $request
  *
  * @return \Dingo\Api\Http\Response
  */
 protected function sendRequestThroughRouter(HttpRequest $request)
 {
     $this->app->instance('request', $request);
     return (new Pipeline($this->app))->send($request)->through($this->middleware)->then(function ($request) {
         return $this->router->dispatch($request);
     });
 }
示例#3
0
文件: Routes.php 项目: lengdelong/api
 /**
  * Create a new routes command instance.
  *
  * @param \Dingo\Api\Routing\Router $router
  *
  * @return void
  */
 public function __construct(Router $router)
 {
     // Ugly, but we need to bypass the constructor and directly target the
     // constructor on the command class.
     Command::__construct();
     $this->routes = $router->getRoutes();
 }
 /**
  * Perform authentication before a request is executed.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure $next
  * @param $grant
  *
  * @return mixed
  * @throws AccessDeniedException
  */
 public function handle($request, Closure $next, $grant = null)
 {
     $route = $this->router->getCurrentRoute();
     /**
      * FOR (Internal API requests)
      * @note GRANT(user) will always be able to access routes that are protected by: GRANT(client)
      *
      * For OAuth grants from password (i.e. Resource Owner: user)
      * @Auth will only check once, because user exists in auth afterwards
      *
      * For OAuth grants from client_credentials (i.e. Resource Owner: client)
      * @Auth will always check, because user is never exists in auth
      */
     if (!$this->auth->check(false)) {
         $this->auth->authenticate($route->getAuthenticationProviders());
         $provider = $this->auth->getProviderUsed();
         /** @var OAuth2 $provider */
         if ($provider instanceof OAuth2) {
             // check oauth grant type
             if (!is_null($grant) && $provider->getResourceOwnerType() !== $grant) {
                 throw new AccessDeniedException();
             }
         }
         // login user through Auth
         $user = $this->auth->getUser();
         if ($user instanceof User) {
             \Auth::login($user);
             event(new UserLoggedInEvent($user));
         }
     }
     return $next($request);
 }
示例#5
0
 /**
  * Attach the after filter to adjust the response.
  *
  * @return void
  */
 protected function attachAfterFilter()
 {
     $this->router->after(function (Request $request, Response $response) {
         $response->headers->set('X-RateLimit-Limit', $this->limiter->getThrottle()->getLimit());
         $response->headers->set('X-RateLimit-Remaining', $this->limiter->getRemainingLimit());
         $response->headers->set('X-RateLimit-Reset', $this->limiter->getRateLimitReset());
     });
 }
示例#6
0
文件: Auth.php 项目: jieke360/api
 /**
  * Perform authentication before a request is executed.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure                 $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $route = $this->router->getCurrentRoute();
     if (!$this->auth->check(false)) {
         $this->auth->authenticate($route->getAuthProviders());
     }
     return $next($request);
 }
示例#7
0
 /**
  * Handle the request.
  *
  * @param \Dingo\Api\Http\Request $request
  * @param \Closure                $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     // To prepare the controller all we need to do is call the current method on the router to fetch
     // the current route. This will create a new Dingo\Api\Routing\Route instance and prepare the
     // controller by binding it as a singleton in the container. This will result in the
     // controller only be instantiated once per request.
     $this->router->current();
     return $next($request);
 }
 /**
  * Define the routes for the application.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function map(Router $router, ApiRouter $apiRouter)
 {
     $apiRouter->version($this->version, function ($apiRouter) use($router) {
         $apiRouter->group(['namespace' => $this->namespace], function ($api) use($router) {
             $router->group(['namespace' => $this->namespace], function ($router) use($api) {
                 require app_path('Http/routes.php');
             });
         });
     });
 }
示例#9
0
 /**
  * Register the router.
  */
 protected function registerRouter()
 {
     $this->app->singleton('api.router', function ($app) {
         $router = new Router($app['Dingo\\Api\\Contract\\Routing\\Adapter'], $app['Dingo\\Api\\Contract\\Debug\\ExceptionHandler'], $app, $this->config('domain'), $this->config('prefix'));
         $router->setConditionalRequest($this->config('conditionalRequest'));
         return $router;
     });
     $this->app->singleton('Dingo\\Api\\Routing\\ResourceRegistrar', function ($app) {
         return new ResourceRegistrar($app['Dingo\\Api\\Routing\\Router']);
     });
 }
示例#10
0
 /**
  * Register the router.
  */
 protected function registerRouter()
 {
     $this->app->singleton('api.router', function ($app) {
         $router = new Router($app[Adapter::class], $app[ExceptionHandler::class], $app, $this->config('domain'), $this->config('prefix'));
         $router->setConditionalRequest($this->config('conditionalRequest'));
         return $router;
     });
     $this->app->singleton(ResourceRegistrar::class, function ($app) {
         return new ResourceRegistrar($app[Router::class]);
     });
 }
示例#11
0
 /**
  * Checks if the user has permission to visit the route.
  * By default it will check if the current route name is in the user permissions.
  * If $permissions is provided, it will check if the $permission value is in the user permissions
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  * @param null|string                      $permission
  * @return mixed
  */
 public function handle($request, Closure $next, $permission = null)
 {
     if (is_null($permission)) {
         $name = $this->router->getCurrentRoute()->getName();
         $permission = is_null($name) ? $this->router->getCurrentRoute()->getActionName() : $name;
     }
     if (Entrust::can($permission)) {
         return $next($request);
     } else {
         return response('Unauthorized.', 401);
     }
 }
示例#12
0
文件: Docs.php 项目: khlipeng/api
 /**
  * Get all the controller instances.
  *
  * @return array
  */
 protected function getControllers()
 {
     $controllers = new Collection();
     foreach ($this->router->getRoutes() as $collections) {
         foreach ($collections as $route) {
             if ($controller = $route->getController()) {
                 $controllers[] = $controller;
             }
         }
     }
     return $controllers;
 }
 public function __construct(RouteCollection $collection, Router $router, Config $config)
 {
     $this->routes = $collection;
     $standardsTree = $config['api.standardsTree'];
     $subtype = $config['api.subtype'];
     $defaultFormat = $config['api.defaultFormat'];
     foreach ($router->getAdapterRoutes() as $versionName => $versionGroup) {
         foreach ($versionGroup as $route) {
             $routeInfo = (new RouteInfo($route, ['router' => 'Dingo', 'version' => $versionName, 'headers' => [['key' => 'Accept', 'value' => "application/{$standardsTree}.{$subtype}.{$versionName}+{$defaultFormat}"]]]))->toArray();
             $this->routes->push($routeInfo);
         }
     }
 }
 /**
  * Merge routes defined by packages into API router.
  *
  * @param  \Dingo\Api\Routing\Router  $api
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 protected function mergePackgeRoutes(ApiRouter &$api, Router $router)
 {
     foreach ($router->getRoutes() as $route) {
         $api->version(array_keys($api->getRoutes()), function ($api) use($route) {
             $action = $route->getAction();
             // Remove prefix if present
             if (isset($action['prefix'])) {
                 unset($action['prefix']);
             }
             $api->addRoute($route->getMethods(), $route->uri(), $action);
         });
     }
 }
示例#15
0
 /**
  * Replace the request input with the previous request input.
  *
  * @return void
  */
 protected function replaceRequestInput()
 {
     array_pop($this->requestStack);
     $previous = end($this->requestStack);
     $this->router->setCurrentRequest($previous);
     $this->request->replace($previous->input());
 }
示例#16
0
文件: RateLimit.php 项目: riclt/api
 /**
  * Perform rate limiting before a request is executed.
  *
  * @param \Dingo\Api\Http\Request $request
  * @param \Closure                $next
  *
  * @throws \Symfony\Component\HttpKernel\Exception\HttpException
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $route = $this->router->getCurrentRoute();
     if ($route->hasThrottle()) {
         $this->handler->setThrottle($route->getThrottle());
     }
     $this->handler->rateLimitRequest($request, $route->getRateLimit(), $route->getRateExpiration());
     if ($this->handler->exceededRateLimit()) {
         throw new HttpException(403, 'You have exceeded your rate limit.', null, $this->getHeaders());
     }
     $response = $next($request);
     if ($this->handler->requestWasRateLimited()) {
         return $this->responseWithHeaders($response);
     }
     return $response;
 }
示例#17
0
 /**
  * Gather the middlewares for the route.
  *
  * @param \Dingo\Api\Http\Request $request
  *
  * @return array
  */
 protected function gatherRouteMiddlewares($request)
 {
     if ($route = $request->route()) {
         return $this->router->gatherRouteMiddlewares($route);
     }
     return [];
 }
示例#18
0
文件: Routes.php 项目: sharenjoy/api
 /**
  * Compile the routes into a displayable format.
  *
  * @return array
  */
 protected function getRoutes()
 {
     $routes = [];
     foreach ($this->router->getRoutes() as $collection) {
         foreach ($collection->getRoutes() as $route) {
             $routes[] = $this->filterRoute(['host' => $route->domain(), 'uri' => implode('|', $route->methods()) . ' ' . $route->uri(), 'name' => $route->getName(), 'action' => $route->getActionName(), 'protected' => $route->isProtected() ? 'Yes' : 'No', 'versions' => implode(', ', $route->versions()), 'scopes' => implode(', ', $route->scopes()), 'rate' => $this->routeRateLimit($route)]);
         }
     }
     if ($sort = $this->option('sort')) {
         $routes = Arr::sort($routes, function ($value) use($sort) {
             return $value[$sort];
         });
     }
     if ($this->option('reverse')) {
         $routes = array_reverse($routes);
     }
     return array_filter(array_unique($routes, SORT_REGULAR));
 }
示例#19
0
文件: Auth.php 项目: jieke360/api
 /**
  * Authenticate the current request.
  *
  * @param array $providers
  *
  * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
  *
  * @return mixed
  */
 public function authenticate(array $providers = [])
 {
     $exceptionStack = [];
     // Spin through each of the registered authentication providers and attempt to
     // authenticate through one of them. This allows a developer to implement
     // and allow a number of different authentication mechanisms.
     foreach ($this->filterProviders($providers) as $provider) {
         try {
             $user = $provider->authenticate($this->router->getCurrentRequest(), $this->router->getCurrentRoute());
             $this->providerUsed = $provider;
             return $this->user = $user;
         } catch (UnauthorizedHttpException $exception) {
             $exceptionStack[] = $exception;
         } catch (BadRequestHttpException $exception) {
             // We won't add this exception to the stack as it's thrown when the provider
             // is unable to authenticate due to the correct authorization header not
             // being set. We will throw an exception for this below.
         }
     }
     $this->throwUnauthorizedException($exceptionStack);
 }
示例#20
0
文件: Docs.php 项目: lengdelong/api
 /**
  * Get all the controller instances.
  *
  * @return array
  */
 protected function getControllers()
 {
     $controllers = new Collection();
     foreach ($this->router->getRoutes() as $collections) {
         foreach ($collections as $route) {
             if ($controller = $route->getController()) {
                 $this->addControllerIfNotExists($controllers, $controller);
             }
         }
     }
     return $controllers;
 }
示例#21
0
文件: Docs.php 项目: rit-sse/api
 /**
  * Get all the controller instances.
  *
  * @return array
  */
 protected function getControllers()
 {
     $controllers = [];
     foreach ($this->router->getRoutes() as $route) {
         $actionName = $route->getActionName();
         if (!empty($actionName) && $actionName !== 'Closure') {
             $segments = explode('@', $actionName);
             $controllers[] = $segments[0];
         }
     }
     $controllerCollection = new Collection(array_unique($controllers));
     return $controllerCollection;
 }
示例#22
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  * @return mixed
  *
  */
 public function handle($request, \Closure $next)
 {
     $route = $this->router->getCurrentRoute();
     if ($route->usesController()) {
         /** @var Controller $controller */
         $controller = $route->getController();
         if (property_exists($controller, 'crsfExclusions') and method_exists($controller, 'getCrsfExclusions')) {
             $excludedMethodNames = $controller->getCrsfExclusions();
             $action = $route->getAction();
             $use = isset($action['uses']) ? $action['uses'] : $action['controller'];
             if (!is_null($use)) {
                 list(, $methods) = explode('@', $use);
                 foreach (explode(',', $methods) as $method) {
                     if (method_exists($controller, $method) and in_array($method, $excludedMethodNames)) {
                         // Skip it!
                         return $next($request);
                     }
                 }
             }
         }
     }
     parent::handle($request, $next);
 }
示例#23
0
文件: Docs.php 项目: vanbrabantf/api
 /**
  * Get all the controller instances.
  *
  * @return array
  */
 protected function getControllers()
 {
     $controllers = new Collection();
     foreach ($this->router->getRoutes() as $collections) {
         foreach ($collections as $route) {
             if ($controller = $route->getController()) {
                 if (!$controllers->has(get_class($controller))) {
                     $controllers->put(get_class($controller), $controller);
                 }
             }
         }
     }
     return $controllers;
 }
示例#24
0
 /**
  * Refresh the request stack.
  *
  * This is done by resetting the authentication, popping
  * the last request from the stack, replacing the input,
  * and resetting the version and parameters.
  *
  * @return void
  */
 protected function refreshRequestStack()
 {
     if (!$this->persistAuthentication) {
         $this->auth->setUser(null);
         $this->persistAuthentication = true;
     }
     if ($route = array_pop($this->routeStack)) {
         $this->router->setCurrentRoute($route);
     }
     $this->replaceRequestInstance();
     $this->clearCachedFacadeInstance();
     $this->raw = false;
     $this->version = $this->domain = $this->content = null;
     $this->parameters = $this->uploads = [];
 }
 /**
  * Replace the bound router.
  *
  * @return void
  */
 protected function replaceBoundRouter()
 {
     $routes = $this->app['router']->getRoutes();
     $this->app->bindShared('router', function ($app) use($routes) {
         $router = new Router($app['events'], $app['api.properties'], $app);
         if ($app['env'] == 'testing') {
             $router->disableFilters();
         }
         $router->setControllerDispatcher(new ControllerDispatcher($router, $app));
         $router->setConditionalRequest($app['config']->get('api::conditional_request'));
         $router->setStrict($app['config']->get('api::strict'));
         $router->addExistingRoutes($routes);
         return $router;
     });
 }
 /**
  * Register and replace the bound router.
  *
  * @return void
  */
 protected function registerRouter()
 {
     $this->app['router'] = $this->app->share(function ($app) {
         $router = new Router($app['events'], $app);
         if ($app['env'] == 'testing') {
             $router->disableFilters();
         }
         return $router;
     });
 }
示例#27
0
 /**
  * Handle the revising of a controller for API requests.
  *
  * @param \Illuminate\Routing\Route $route
  * @param \Illuminate\Http\Request  $request
  *
  * @return void
  */
 public function handle(Route $route, Request $request)
 {
     if ($this->router->isApiRequest($request)) {
         $this->reviser->revise($route);
     }
 }
示例#28
0
 /**
  * Register the router.
  *
  * @return void
  */
 protected function registerRouter()
 {
     $this->app->singleton('api.router', function ($app) {
         $config = $app['config']['api'];
         $router = new Router($app['api.router.adapter'], new Http\Parser\Accept($config['standardsTree'], $config['subtype'], $config['version'], $config['defaultFormat']), $app['Dingo\\Api\\Contract\\Debug\\ExceptionHandler'], $app, $config['domain'], $config['prefix']);
         $router->setConditionalRequest($config['conditionalRequest']);
         return $router;
     });
     $this->app->singleton('Dingo\\Api\\Routing\\ResourceRegistrar', function ($app) {
         return new ResourceRegistrar($app['api.router']);
     });
 }
示例#29
0
 /**
  * Replace the request instance with the previous request instance.
  *
  * @return void
  */
 protected function replaceRequestInstance()
 {
     array_pop($this->requestStack);
     $this->container->instance('request', end($this->requestStack));
     $this->router->setCurrentRequest($this->container['request']);
 }
 public function map(\Dingo\Api\Routing\Router $api)
 {
     $api->version(['version' => 'v1'], function ($api) {
         return require app_path('Api/v1/Http/routes.php');
     });
 }