Why you can’t have a solid application using laravel

<?php

namespace App\Http\Controllers;
use App\Models\Post;class PostController extends Controller
{
public function show(Post $post)
{
return view('post.show', ['post' => $post]);
}
}
<?php

namespace App\Http\Controllers;
use App\Models\Post;class PostController extends Controller
{
public function show($id,PostRepositoryInterface $postRepository)
{
$post = $postRepository->find($id);
return view('post.show', ['post' => $post]);
}
}
<div>{{$post->delete()}}</div>
<?php

namespace App\Controller;

use App\Service\PostRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class PostController extends AbstractController
{
#[Route('/post/{id}')]
public function show(int $id, PostRepositoryInterface $postRepository): Response
{
$post = $postRepository->find($id);
//$post->delete() would throw undefined exception
return $this->render('post/show.html.twig', [
'title' => $post->getTitle(),
]);
}
}
<?php

namespace App\Entity;
// note that the orm and the repository is only being used as annotations
use App\Repository\PostRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: PostRepository::class)]
class Post
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;

#[ORM\Column(length: 255)]
private ?string $title = null;

public function getId(): ?int
{
return $this->id;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store