Skip to content

Commit

Permalink
Update 2023-07-01-mvc-laravel.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lightszentip committed Jul 1, 2023
1 parent 33a2dd0 commit a70bb94
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions webpage/content/post/2023-07-01-mvc-laravel.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ composer create-project --prefer-dist laravel/laravel example-mvc-app
Next, we create a model for our blog articles using the Artisan command:

```
php artisan make:model Artikel
php artisan make:model Article
```

## Step 2: Migration and Database Setup

We create a migration to generate the table for our blog articles in the database:

```
php artisan make:migration create_artikel_table
php artisan make:migration create_articlel_table
```

Open the newly created migration file in the `database/migrations` folder and define the columns for the blog articles table. Then, execute the migration:
Expand All @@ -57,26 +57,26 @@ Now, we create a controller to manage actions for our blog articles:
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repositories\ArtikelRepository;
use App\Repositories\ArticleInterfaceRepository;

class ArtikelController extends Controller
class ArticleController extends Controller
{
private $artikelRepository;
private ArticleInterfaceRepository $articleRepository;

public function __construct(ArtikelRepository $artikelRepository)
public function __construct(ArticleInterfaceRepository $articleRepository)
{
$this->artikelRepository = $artikelRepository;
$this->articleRepository = $articleRepository;
}

public function index()
{
$artikel = $this->artikelRepository->getAll();
$artikel = $this->articleRepository->getAll();
return view('artikel.index', ['artikel' => $artikel]);
}

public function show($id)
{
$artikel = $this->artikelRepository->getById($id);
$artikel = $this->articleRepository->getById($id);
return view('artikel.show', ['artikel' => $artikel]);
}

Expand All @@ -87,25 +87,25 @@ class ArtikelController extends Controller

public function store(Request $request)
{
$this->artikelRepository->create($request->all());
$this->articleRepository->create($request->all());
return redirect()->route('artikel.index');
}

public function edit($id)
{
$artikel = $this->artikelRepository->getById($id);
$artikel = $this->articleRepository->getById($id);
return view('artikel.edit', ['artikel' => $artikel]);
}

public function update(Request $request, $id)
{
$this->artikelRepository->update($id, $request->all());
$this->articleRepository->update($id, $request->all());
return redirect()->route('artikel.index');
}

public function destroy($id)
{
$this->artikelRepository->delete($id);
$this->articleRepository->delete($id);
return redirect()->route('artikel.index');
}
}
Expand All @@ -115,14 +115,14 @@ class ArtikelController extends Controller

Next, we create a repository class to handle database queries for our blog articles:

First, we create an interface for our ArtikelRepository to define the contract that our repository class must implement. Create a new file ArtikelRepositoryInterface.php in the app/Repositories folder:
First, we create an interface for our ArticleRepository to define the contract that our repository class must implement. Create a new file ArticleInterfaceRepository.php in the app/Repositories folder:

```php
// app/Repositories/ArtikelRepositoryInterface.php
// app/Repositories/ArticleInterfaceRepository.php

namespace App\Repositories;

interface ArtikelRepositoryInterface
interface ArticleInterfaceRepository
{
public function getAll();

Expand All @@ -142,9 +142,9 @@ interface ArtikelRepositoryInterface

namespace App\Repositories;

use App\Models\Artikel;
use App\Models\Article;

class ArtikelRepository implements ArtikelRepositoryInterface
class ArticleRepository implements ArticleInterfaceRepository
{
public function getAll()
{
Expand Down Expand Up @@ -185,10 +185,12 @@ And add the following code:
```
public function register()
{
$this->app->bind(ArtikelRepositoryInterface::class, ArtikelRepository::class);
$this->app->bind(ArticleInterfaceRepository::class, ArticleRepository::class);
}
```

Annd add the provider into "config/app.php" to the list of providers.

## Step 6: Testing the Architecture

Now that our architecture is implemented, we can ensure everything works by testing our application.
Expand Down

0 comments on commit a70bb94

Please sign in to comment.