Skip to content

Instantly share code, notes, and snippets.

@jpalala
Forked from tomgrohl/README.md
Last active October 18, 2015 15:35
Show Gist options
  • Save jpalala/401b7d188a97b2d5d5b7 to your computer and use it in GitHub Desktop.
Save jpalala/401b7d188a97b2d5d5b7 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.

#[CHANGES in L4.2]#

  1. You'll need to swap out Illuminate\Foundation\Application see http://stackoverflow.com/a/27111495
  2. You'll need to namespace the classes below if you'll include it in composer.json's "classmap".
  3. Watch out - I hardcoded the secure url part false because Config wasn't loading. So this is still incomplete.

updated by jpalala

  • 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 namespace <PutYourNameSpace>;
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 namespace <PutYourNameSpace>;
use Illuminate\Routing\Router;
use Illuminate\Routing\Redirector;
use Illuminate\Support\ServiceProvider;
use <YourNameSpace>\UrlGenerator; //so new UrlGenerator calls the class you created in your namespace
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);
//in L4.2 it you need the events now
$router = new Router($app['events'], $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 namespace <PutYourNameSpace>
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())
{
//Couldnt get Config object in here, for some reason, hence the work-around
if(!Config)
$secure = false;
else
$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