PHP code example of martynbiz / slim3-controller

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

    

martynbiz / slim3-controller example snippets


composer 



// index routes (homepage, about, etc)
$app->group('', function () use ($app) {

    $controller = new App\Controller\IndexController($app);

    $app->get('/', $controller('index'));
    $app->get('/contact', $controller('contact'));
});

// create resource method for Slim::resource($route, $name)
$app->group('/articles', function () use ($app) {

    $controller = new App\Controller\ExampleController($app);

    $app->get('', $controller('index'));
    $app->get('/create', $controller('create'));
    $app->post('', $controller('post'));
    $app->get('/{id:[0-9]+}', $controller('show'));
    $app->get('/{id:[0-9]+}/edit', $controller('edit'));
    $app->put('/{id:[0-9]+}', $controller('put'));
    $app->delete('/{id:[0-9]+}', $controller('delete'));
});



namespace App\Controller;

use MartynBiz\Slim3Controller\Controller;

class ExampleController extends Controller
{
    public function index()
    {
        return $this->render('admin/example/index.html', array(
            // data to pass to the view
        ));
    }

    public function show($id)
    {
        return $this->render('admin/example/show.html', array(
            // data to pass to the view
        ));
    }

    public function create()
    {
        return $this->render('admin/example/create.html');
    }

    public function post()
    {
        // handle create

        return $this->redirect('/admin/example');
    }

    public function edit($id)
    {
        return $this->render('admin/example/edit.html', array(
            // data to pass to the view
        ));
    }

    public function update($id)
    {
        // handle update

        return $this->redirect('/admin/example/' . $id);
    }
}

$container['model.example'] = function ($container) {
    return new App\Model\Example();
};

.
.
.
class ExampleController extends Controller
{
    public function index()
    {
        // the "get" method is used to retrieve items stored in the Slim container
        $examples = $this->get('model.example')->find();

        // the "render" provides a neat means to pass template and data to $container['view']
        return $this->render('admin/example/index.html', array(
            'examples' => $examples,
        ));
    }
    .
    .
    .

// coming soon, still a little bloated



use SlimMvc\Test\PHPUnit\TestCase;

class ExampleControllerTest extends TestCase
{
    /**
     * @var Slim\Container
     */
    protected $container;

    public function setUp()
    {
        // =========================
        // Instantiate the app and container

        $settings = come "frozen", we need to define
        // mocks before they are loaded, so immediately after including dependencies.php is best

        //...
        $this->container['my_dependency'] = ...


        // =========================
        // Register middleware

        );
        $this->assertQuery('table#examples');
        $this->assertQueryCount('div.errors', 0);
        // $this->assertRedirects();
        // $this->assertRedirectsTo('...');
    }
}

$request->getCookie($name, $defaultValue);