samedi 18 février 2017

L5 Using Hashid in middleware returns null

I am using hashid to hash the id parameters in url. I have it set up in my model to automatically hash the id. This is working fine. My problem is decoding the hash in a middleware returns null. I'm not sure if this is a problem with my middleware or because of the hashing.

Model:

public function getIdAttribute($value)
    {
        $hashids = new \Hashids\Hashids(env('APP_KEY'),10);
        return $hashids->encode($value);
    }

Middleware:

<?php

namespace App\Http\Middleware;

use Closure;

class HashIdsDecode
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        dd($request->id); //Returns null on show method - example localhost:8000/invoices/afewRfshTl
        if($request->has('id'))
        {
            $hashids = new \Hashids\Hashids(env('APP_KEY'),10);
            dd($hashids->decode($request->input('id')));
        }

        return $next($request);
    }
}

Route:

Route::resource('invoices','InvoiceController');

Controller:

public function show($id)
    {
        $invoice = Invoice::find($id);
        return view('invoices.show', [
            'invoice' => $invoice,
            'page_title' => ' Invoices',
            'page_description' => 'View Invoice',
        ]);
    }

NOTE: if I bypass the middleware and do it directly in my controller like this it works but it requires me to repeat myself over and over and probably not the best way to do it.

public function show($id)
    {
        $hashids = new \Hashids\Hashids(env('APP_KEY'),10);
        $invoiceId = $hashids->decode($id)[0];
        $invoice = Invoice::find($invoiceId);

        return view('invoices.show', [
            'invoice' => $invoice,
            'page_title' => ' Invoices',
            'page_description' => 'View Invoice',
        ]);
    }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kLBG3s
via IFTTT

Aucun commentaire:

Enregistrer un commentaire