Skip to content

Instantly share code, notes, and snippets.

@xtrasmal
Created May 12, 2017 23:37
Show Gist options
  • Save xtrasmal/395459f4a6ad21879f03b1e6a47f08db to your computer and use it in GitHub Desktop.
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.
<?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