PHP code example of felipecarvalho-back / forge-mvc

1. Go to this page and download the library: Download felipecarvalho-back/forge-mvc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

felipecarvalho-back / forge-mvc example snippets


// routes/web.php
Route::get('/produtos', [ProdutoController::class, 'index'])->name('produtos.index');
Route::post('/produtos', [ProdutoController::class, 'store']);

// app/Controllers/ProdutoController.php
class ProdutoController extends Controller
{
    public function index(): Response
    {
        $produtos = (new Produto())->orderBy('nome')->get();
        return view('produtos/index', compact('produtos'));
    }

    public function store(): Response
    {
        $data = validate(new ProdutoDto());
        (new Produto())->insert($data);
        return redirect(route('produtos.index'));
    }
}

// app/Models/Produto.php
class Produto extends Model
{
    protected array $fillable = ['nome', 'preco', 'categoria_id'];
    protected array $hidden   = ['custo_interno'];

    public function categoria(): ?Categoria
    {
        return $this->belongsTo(Categoria::class, 'categoria_id');
    }
}