Skip to content

Commit

Permalink
Move custom redirector to core
Browse files Browse the repository at this point in the history
This was previously found in the User plugin and is a black box; nearly impossible to find from the `Redirect` facade, the `october\rain` library should  provide an interface to change the key name, that is, if Laravel doesn't add this common feature in a later version
  • Loading branch information
daftspunk committed Dec 10, 2024
1 parent cbed208 commit 9ba432e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public function registerCoreContainerAliases()
'cache.psr6' => [\Symfony\Component\Cache\Adapter\Psr16Adapter::class, \Symfony\Component\Cache\Adapter\AdapterInterface::class, \Psr\Cache\CacheItemPoolInterface::class],
'config' => [\Illuminate\Config\Repository::class, \Illuminate\Contracts\Config\Repository::class],
'cookie' => [\Illuminate\Cookie\CookieJar::class, \Illuminate\Contracts\Cookie\Factory::class, \Illuminate\Contracts\Cookie\QueueingFactory::class],
'db' => [\October\Rain\Database\DatabaseManager::class],
'db' => [\Illuminate\Database\DatabaseManager::class],
'db.connection' => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class],
'db.schema' => [\Illuminate\Database\Schema\Builder::class],
'encrypter' => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class],
Expand All @@ -347,7 +347,7 @@ public function registerCoreContainerAliases()
'queue' => [\Illuminate\Queue\QueueManager::class, \Illuminate\Contracts\Queue\Factory::class, \Illuminate\Contracts\Queue\Monitor::class],
'queue.connection' => [\Illuminate\Contracts\Queue\Queue::class],
'queue.failer' => [\Illuminate\Queue\Failed\FailedJobProviderInterface::class],
'redirect' => [\Illuminate\Routing\Redirector::class],
'redirect' => [\October\Rain\Router\CoreRedirector::class],
'redis' => [\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class],
'redis.connection' => [\Illuminate\Redis\Connections\Connection::class, \Illuminate\Contracts\Redis\Connection::class],
'request' => [\Illuminate\Http\Request::class, \Symfony\Component\HttpFoundation\Request::class],
Expand Down
54 changes: 54 additions & 0 deletions src/Router/CoreRedirector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php namespace October\Rain\Router;

use App;
use Illuminate\Routing\Redirector as RedirectorBase;

/**
* CoreRedirector adds extra events to the base redirector and ensures the "intended"
* session is different for the frontend and backend contexts.
*
* @package october\router
* @author Alexey Bobkov, Samuel Georges
*/
class CoreRedirector extends RedirectorBase
{
/**
* intended creates a new redirect response to the previously intended location.
* @return \Illuminate\Http\RedirectResponse
*/
public function intended($default = '/', $status = 302, $headers = [], $secure = null)
{
if (!App::runningInFrontend()) {
return parent::intended($default, $status, $headers, $secure);
}

$path = $this->session->pull('url.cms.intended', $default);

return $this->to($path, $status, $headers, $secure);
}

/**
* getIntendedUrl from the session.
*/
public function getIntendedUrl()
{
if (!App::runningInFrontend()) {
return parent::getIntendedUrl();
}

return $this->session->get('url.cms.intended');
}

/**
* setIntendedUrl in the session.
*/
public function setIntendedUrl($url)
{
if (!App::runningInFrontend()) {
return parent::setIntendedUrl($url);
}

$this->session->put('url.cms.intended', $url);
return $this;
}
}
19 changes: 19 additions & 0 deletions src/Router/RoutingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,23 @@ protected function registerRouter()
return new CoreRouter($app['events'], $app);
});
}

/**
* registerRedirector
*/
protected function registerRedirector()
{
$this->app->singleton('redirect', function ($app) {
$redirector = new CoreRedirector($app['url']);

// If the session is set on the application instance, we'll inject it into
// the redirector instance. This allows the redirect responses to allow
// for the quite convenient "with" methods that flash to the session.
if (isset($app['session.store'])) {
$redirector->setSession($app['session.store']);
}

return $redirector;
});
}
}

0 comments on commit 9ba432e

Please sign in to comment.