public function run() { $router = new \Illuminate\Routing\Router(new \Illuminate\Events\Dispatcher($this->container), $this->container); $router->get('/', \platzi\Http\Controllers\HomeController::class . '@index'); $router->get('/post/{id}', \platzi\Http\Controllers\HomeController::class . '@show'); //$request = Request::capture(); $response = $router->dispatch(\Illuminate\Http\Request::capture()); $response->send(); }
/** * Resolve a parameter value for the route. * * @param string $key * @return mixed */ protected function resolveParameter($key) { $value = $this->parameters[$key]; // If the parameter has a binder, we will call the binder to resolve the real // value for the parameters. The binders could make a database call to get // a User object for example or may transform the input in some fashion. if ($this->router->hasBinder($key)) { return $this->router->performBinding($key, $value, $this); } return $value; }
/** * Limit the HTTP request according to the rates. * * @param Illuminate\Http\Request $request * @return Illuminate\Http\Request */ public function limit(Request $request) { // Remember the old cache settings to reset later $oldDriver = $this->config->get('cache.driver'); $oldTable = $this->config->get('cache.table'); // Setup cache to use cache table $driver = $this->config->get($this->namespace . '::core.rates.cache.driver', 'database'); $table = $this->config->get($this->namespace . '::core.rates.cache.table', 'cache'); $this->config->set('cache.driver', $driver); $this->config->set('cache.table', $table); // Rate limit by IP address $tag = $this->config->get($this->namespace . '::core.rates.cache.tag', 'xrate:'); $tag = sprintf($tag . ':%s', $request->getClientIp()); // Rate limit by route address if ($this->config->get($this->namespace . '::core.rates.routes') && $this->router->current()) { // Add the route name to the rate tag $tag = sprintf($tag . ':%s', $this->router->currentRouteName()); // Add the route parameters to the rate tag $parameters = $this->router->current()->parameters(); if (!empty($parameters) && $this->config->get($this->namespace . '::core.rates.parameters')) { $tag = sprintf($tag . ':%s', implode(',', $parameters)); } } // Set the tag that is used $this->tag = $tag; // Prime rate limiter as a tagged cache $this->cache->add($tag, 0, $this->getPeriod()); // Increment rate limiter count for current request $this->counter = (int) $this->cache->get($tag); // Increment counter if ($this->getCounter() < $this->getLimit()) { $this->counter++; $this->cache->put($tag, $this->getCounter(), $this->getPeriod()); } elseif (!$this->cache->has($tag . ':timeout')) { // Add timeout $this->cache->add($tag . ':timeout', true, $this->getTimeout()); // Fire event listener $ip = $request->getClientIp(); $route = $this->router->currentRouteName(); $limit = $this->getLimit(); $timeout = $this->getTimeout(); $this->events->fire('esensi.core.rate_exceeded', compact('ip', 'route', 'limit', 'timeout')); } // Determine if request is in timeout $this->inTimeout = $this->cache->has($tag . ':timeout'); // Reset cache settings $this->config->set('cache.driver', $oldDriver); $this->config->set('cache.table', $oldTable); return $request; }