Created
May 12, 2017 23:37
-
-
Save xtrasmal/395459f4a6ad21879f03b1e6a47f08db to your computer and use it in GitHub Desktop.
Playing with route bindings and hashids to get a backwards compatible solution. It will not decode if it can't.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// create a route like this | |
$this->route->get('projects/{id}', 'ProjectsController@show'); | |
// Put this in your provider | |
// It will check if it can decode, else it will just return what was put in the url | |
$this->route->bind('id', function ($id) { | |
return (new Hashids())->decode($id)[0] ?? $id; | |
}); | |
// Example: | |
// 1). Not encoded: http://site.com/projects/3 | |
public function show($id) | |
{ | |
return $id; // will return 3 | |
} | |
// 2). Encoded: http://site.com/projects/j5 | |
public function show($id) | |
{ | |
return $id; // will return 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment