Slug controlar excepciones

 Crear en App/Exception/InvalidEntrySlugException.php:


<?php

namespace App\Exceptions;


use App\Entry;

use Exception;

use Throwable;


class InvalidEntrySlugException extends Exception {

    private $entry;


    public function __construct(Entry $entry, $message = "", $code = 0, Throwable $previous = null)

    {

        $this->entry = $entry;

        parent::__construct($message, $code, $previous);

    }


    public function render()

    {

        return redirect($this->entry->getUrl());

    }

}

?>


En App/providers/RouteServiceProvider.php

$entry = Entry::findOrFail($id);

if ($entry->slug."-".$entry->id === $value) {

    return $entry;

}

else {

   // forzará a q se vaya a la url correcta por si se pone lo-xxxx-viento-se-llevo-3

   throw new InvalidEntrySlugException($entry);

}


No hay comentarios:

Publicar un comentario