Skip to content

Instantly share code, notes, and snippets.

@tomgrohl
Last active November 16, 2016 08:02
Show Gist options
  • Save tomgrohl/8068212 to your computer and use it in GitHub Desktop.
Save tomgrohl/8068212 to your computer and use it in GitHub Desktop.
Make Url::secure in Laravel 4 work like Laravel 3

Make URL::secure/Redirect::secure work in Laravel 4 like it did in Laravel 3

In Laravel 3 URL::secure/Redirect::secure got overridden by the ssl config. Nice for testing locally when you don't have ssl setup.

In Laravel 4 they took it out. Why? Fuck knows...

Anyway heres how to get it working again.

  • Step 1

    Add ssl config to config/app.php

    'ssl' => false, // Or true depending on your environment
  • Step 2

    Add the classes below to a folder that autoloads classes (I added a classes folder to the app folder and added this to composer)

  • Step 3

    Add the following to your providers array in config/app.php (Add it to the bottom of the array)

    'RoutingServiceProvider',
  • Step 4

    Run composer dump-autoload

  • Step 5

    Mexican wave!

<?php
class Redirector extends \Illuminate\Routing\Redirector
{
/**
* Create a new redirect response to the given HTTPS path.
*
* @param string $path
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function secure($path, $status = 302, $headers = array())
{
$secure = Config::get('app.ssl');
return $this->to($path, $status, $headers, $secure);
}
}
<?php
use Illuminate\Routing\Router;
use Illuminate\Routing\Redirector;
use Illuminate\Support\ServiceProvider;
class RoutingServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerRouter();
$this->registerUrlGenerator();
$this->registerRedirector();
}
/**
* Register the router instance.
*
* @return void
*/
protected function registerRouter()
{
$this->app['router'] = $this->app->share(function($app)
{
$router = new Router($app);
// If the current application environment is "testing", we will disable the
// routing filters, since they can be tested independently of the routes
// and just get in the way of our typical controller testing concerns.
if ($app['env'] == 'testing')
{
$router->disableFilters();
}
return $router;
});
}
/**
* Register the URL generator service.
*
* @return void
*/
protected function registerUrlGenerator()
{
$this->app['url'] = $this->app->share(function($app)
{
// The URL generator needs the route collection that exists on the router.
// Keep in mind this is an object, so we're passing by references here
// and all the registered routes will be available to the generator.
$routes = $app['router']->getRoutes();
return new \UrlGenerator($routes, $app['request']);
});
}
/**
* Register the Redirector service.
*
* @return void
*/
protected function registerRedirector()
{
$this->app['redirect'] = $this->app->share(function($app)
{
$redirector = new \Redirector($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;
});
}
}
<?php
class UrlGenerator extends \Illuminate\Routing\UrlGenerator {
/**
* Generate a secure, absolute URL to the given path.
*
* @param string $path
* @param array $parameters
* @return string
*/
public function secure($path, $parameters = array())
{
$secure = Config::get('app.ssl');
return $this->to($path, $parameters, $secure);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment