Improve exception handling and model binding

This commit is contained in:
Jonny Barnes 2020-08-09 15:54:10 +01:00
parent e9ca934cb4
commit 0fca80e7e4
22 changed files with 148 additions and 82 deletions

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Http\Controllers;
use App\Models\Contact;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\View;
@ -13,7 +14,7 @@ class ContactsController extends Controller
/**
* Show all the contacts.
*
* @return \Illuminate\View\View
* @return View
*/
public function index(): View
{
@ -34,17 +35,15 @@ class ContactsController extends Controller
/**
* Show a single contact.
*
* @todo Use implicit model binding.
*
* @param string $nick The nickname associated with contact
* @return \Illuminate\View\View
* @param Contact $contact
* @return View
*/
public function show(string $nick): View
public function show(Contact $contact): View
{
$filesystem = new Filesystem();
$contact = Contact::where('nick', '=', $nick)->firstOrFail();
$contact->homepageHost = parse_url($contact->homepage, PHP_URL_HOST);
$file = public_path() . '/assets/profile-images/' . $contact->homepageHost . '/image';
$filesystem = new Filesystem();
$image = ($filesystem->exists($file)) ?
'/assets/profile-images/' . $contact->homepageHost . '/image'
: