Skip to content

Instantly share code, notes, and snippets.

@kristopolous
Last active July 22, 2016 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristopolous/430b4d86a9b9f32f2b9edc6d768441b0 to your computer and use it in GitHub Desktop.
Save kristopolous/430b4d86a9b9f32f2b9edc6d768441b0 to your computer and use it in GitHub Desktop.
Getting Input to work in laravel5

Laravel 5 decided to remove the global Input class. What are the implications to existing code bases? As usual the answer is "eh who cares? fuck em."

How does a responsible adult deal with a bunch of finickey novelty-obsessed designers who change specs and pull the rug out from you every 6 months?

Unfortunately I have no general answer to this confounding and dumbfounding lack of even the most basic discipline, but in the case of Input I do!

Adding a backwards-compatible Input into your laravel5 project

This can be reliably achieved through middleware.

This probably isn't the "right" way of doing things, but if you are one of those dweebs advocating to break every thing every month then sorry, you don't get a seat at the table.

So we first make a middleware file using artisan:

$ php artisan make:middleware PutInputBackIn

This should create ./app/Http/Middleware/PutInputBackIn.php. Make it look like:

<?php

namespace App\Http\Middleware;

use Closure;

class PutInputBackIn
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
	\Input::$request = $request;
	// ^^ this is the magic added line...
	return $next($request);
    }
}

?>

And that's it! :-)

No I kid I kid, can you imagine?

Adding the middleware

Anyway now that you've written the middleware you have to use it.

Let's go to the documentation where it says "you may easily assign middleware to the controller".^1

Well Of Course this is utter bullshit and doesn't work at all. See there's this insanely bizarre perversion of class loading that laravel is BFFs with.

Instead of the incorrect

$this->middleware('auth');

Put the full namespace in there (duh):

$this->middleware('App\Http\Middleware\PutInputBackIn');

Qwestion: Can't you just put use App\Http\Middleware at the top of the file and not do this?!

Answer! NO! Because that is run in a parent class somewhere in deep framework-land that only cares about the 'use' statement at the top of its file.

Anyway, so put the full path in ... AND THEN WE ARE ALMOST THERE!

The next thing you have to do is get our Input class in there ... which you can force in through composer.json (things have been added here for context to tell you where things go ... take note, laravel doc writers):

"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~4.0",
    "symfony/css-selector": "2.8.*|3.0.*",
    "symfony/dom-crawler": "2.8.*|3.0.*"
},
VVVV THIS PART RIGHT HERE VVVV
"autoload": {
    "files": [
        "app/laravel3to5.php"
    ],
^^^ YES THAT PART ^^^^
    "classmap": [
        "database",
        "app/Http/Controllers",
        "app/Models"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

And now create app/laravel3to5.php:

<?php
class Input {
    public static $request;
    public static function __call($name, $arguments) {
      return call_user_func_array([static::$request, $name], $arguments);
    }
}
?>

then:

$ composer dumpautoload

All your old Input code should now work.

And if it doesn't, there's comments below... yes, commenting! You can refute this documentation as bullshit. Imagine that!

[1] I could use the words like "easy" or "simply" above if I was writing documentation, you know, to thoroughly insult you when in reality my instructions were incorrect and untested. But since I'm a grown-up, I'm not going to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment