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/ */
// 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');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.