PHP code example of tychovbh / laravel-mvc

1. Go to this page and download the library: Download tychovbh/laravel-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/ */

    

tychovbh / laravel-mvc example snippets


$app->register(\Tychovbh\Mvc\MvcServiceProvider::class);

class User extends model
{
    protected $filled = ['name']
}

// Get all
$this->repository->all();

// Search all resources where name: Jan
$this->repository::withParams(['name' => 'Jan'])->get();

// Search all resources where name: Jan and get first.
$this->repository::withParams(['name' => 'Jan'])->first();

// Search all resources where names in: Jan and piet
$this->repository::withParams(['name' => ['jan', 'piet']])->get();

// Order all resources by name or any other Laravel statement
$this->repository::withParams(['sort' => 'name desc')]->get();

// Paginate 10
$this->repository->paginate(10);

// Paginate 10 where country in Netherlands or Belgium.
$this->repository::withParams(['country' => ['Netherlands', 'Belgium']])->paginate(4);

// Search resource with ID: 1
$this->repository->find(1);

// Store resource in the database. This uses laravel fill make sure you add protected $filled = ['name'] to your User model.
$user = $this->repository->save(['name' => 'jan']);

// Update resource.
$user = $this->repository->update(['name' => 'piet'], 1);

// Destroy resource(s).
$this->repository->destroy([1]);

class UserRepository extends AbstractRepository implements Repository
{
    public function find(int $id)
    {
        // add your own implementation of find
        return $user;
    }
    
    public function save($data)
    {
        // Add some logic and then call parent save
        $data['password'] = Hash:make($data['password']);
        return parent::save($data);
    }
    
    // You can add your own custom params to filter the request
    // This will be triggered when key is "search" is added to the params:
    // Let's say we want to build a search on firstname, lastname and email:
    // $repository->params(['search' => 'jan'])->all();
    // $repository->params(['search' => '[email protected]'])->all();
    // $repository->params(['search' => 'piet'])->all();
    // We can do that by adding a method, just capitalize the param key and add index{key}Param to the method name.
    public function indexSearchParam(string $search)
    {
        $this->query->where('email', $search)
                    ->orWhere('firstname', $search)
                    ->orWhere('surname', $search);    
    }
    
    // You can do the same for show methods like find
    public function showSearchParam(string $search);
}

 php
class UserController extends AbstractController
{
    public function __construct(UserRepository $repository)
    {
        $this->repository = $repository
    }
    
    public function index()
    {
        $users = $this->repository->all();
        return response()->json($users)
    }
}
 php
class UserController extends AbstractController
{
    public function index()
    {
        // Do stuff before querying
        
        $response = parent::index();

        // Do stuff after querying

        return $response;
    }
}
 php
// routes/web.php (Laravel)
$router->post('/users', 'UserController@index')
->name('user.index')
->middleware('validate');

// routes/web.php (Lumen)
$router->post('/users', [
    'middleware' => 'validate',
    'as' => 'users.index',
    'uses' => 'UserController@index'
]);