php - Laravel 5 路由绑定(bind)和 Hashid

标签 php routes laravel-5 hashids

我在 Laravel 5 中使用 Hashid 来隐藏资源的 ID。

这是路由文件中的路由绑定(bind):

Route::bind('schedule', function($value, $route)
{
    $hashids = new Hashids\Hashids(env('APP_KEY'),8);
    if( isset($hashids->decode($value)[0]) )
    {
        $id = $hashids->decode($value)[0];
        return App\Schedule::findOrFail($id);
    }
    App::abort(404);
});

在模型中:

public function getRouteKey()
{
    $hashids = new \Hashids\Hashids(env('APP_KEY'),8);
    return $hashids->encode($this->getKey());
}

现在这工作正常,资源显示完美,ID 被散列。 但是当我转到我的创建路径时,它是 404 - 如果我删除 App::abort(404),创建路径将转到资源“显示” View ,没有任何数据...

这是创建路径:

Route::get('schedules/create', [
  'uses' => 'SchedulesController@create',
  'as' => 'schedules.create'
]);

演出路线:

Route::get('schedules/{schedule}', [
  'uses' => 'Schedules Controller@show',
  'as' => 'schedules.show'
]);

我还将模型绑定(bind)到路线:

Route::model('schedule', 'App\Schedule');

知道为什么我的创建 View 没有正确显示吗?索引 View 显示正常。

最佳答案

原来要解决这个问题,我不得不重新安排我的 crud 路线。

创建需要在 Show 路由之前...

关于php - Laravel 5 路由绑定(bind)和 Hashid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30992452/

相关文章:

php - Auth::login() 无法正常工作 laravel 5.7

php - 使用变量中的电子邮件地址发送邮件

php - 从行中选择最大的数字

php - 如何在 PHP 代码中插入笑脸?

node.js - 如何将代码与请求处理程序分离(node.js 和express.js)

go - Go 中的路由器 - 在每个 http 请求之前运行一个函数

php - 调试 php.ini 语法错误

php - 使用php以正则表达式条件搜索数据库

ruby-on-rails - 仅针对某些操作的自定义路由路径

php - 如何使自增列不自增?