PHP code example of raphhh / puppy-application

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

    

raphhh / puppy-application example snippets


use Puppy\Application;
use Puppy\Config\Config;
use Symfony\Component\HttpFoundation\Request;

$puppy = new Application(new Config(), Request::createFromGlobals());
$puppy->get('hello', function(){ 
    return 'Hello world!';
});
$puppy->run(); //good dog! :)

$puppy->get($uri, $controller); //filter on GET http method
$puppy->post($uri, $controller); //filter on POST http method
$puppy->json($uri, $controller); //filter on JSON format
$puppy->any($uri, $controller); //filter only on the requested uri
$puppy->filter($filter, $controller); //specific filter as callable

$puppy->get('my/page/(\d)', $controller); 

$puppy->get('my/specific/:uri', $controller)->bind('uri'); 

$puppy->get('my/page/:index', $controller)->bind('index', '\d'); 

$puppy->get(':all', $controller); //every uri
$puppy->get(':home', $controller); //home uri (empty or '/')
$puppy->get(':slug', $controller); //string uri, with numeric, '_' and '-'
$puppy->get(':id', $controller); //any unsigned int, except 0
$puppy->get(':index', $controller); //any unsigned int
$puppy->get(':lang', $controller); //two letters lower case, eventually followed by hyphen and two letters upper case (e.i. fr-FR)
$puppy->get(':datetime', $controller); //datetime with format yyyy-mm-ddThh:mm:ss or yyyy-mm-ddThh:mm:ss+hh:ss
$puppy->get(':date', $controller); //date with format yyyy-mm-dd
$puppy->get(':time', $controller); //time with format hh:mm:ss

$puppy->get($uri, $controller)->content('xml/application');
$puppy->json($uri, $controller)->method('post');

$puppy->any('my/page/:index', $controller)
    ->bind('index', '\d')
    ->method('post')
    ->content('json/application');    

$puppy->get('users', $controller)->restrict('admin'); // this is accessible only with the request uri 'admin/users'

$puppy->group([
             $puppy->get($uri1, $controller1),
             $puppy->get($uri2, $controller2),
        ])
      ->bind('index', '\d')
      ->method('post')
      ->restrict('admin');


$puppy->get('hello', function(){
    ...
});

$puppy->get('hello', array($controller, 'method'));

$puppy->get('hello', function(){
    return '<h1>Hello world!</h1>';
});

$puppy->get('hello', function(){
    return new Response('<h1>Hello world!</h1>');
});

$puppy->get('hello/:all', function(array $args){
    return $args['all']; //will return the value "world" for the uri "/hello/world"
});

$puppy->get('hello', function(\ArrayAccess $services){
    ...
});

$puppy->get('hello', function(array $args, Container $services){
    ...
});

$puppy->get('hello', function(Request $request){
    return 'You ask for the uri "'.htmlentities($request->getRequestUri());
});

$appController->render($templateFile);
$appController->redirect($url);
$appController->call($uri);
$appController->abort();
$appController->flash()->get($myMessage);
$appController->retrieve($key);
$appController->getService($serviceName);

$puppy->get('hello', function(){
    return $this->abort();
});

use Puppy\Controller\AppController;

class MyController extends AppController 
{    
    public function myAction()
    {
        return $this->abort();
    }
} 

$puppy->get('hello', function(AppController $appController){
    return $appController->abort();
});

$puppy->get($uri, $controller)->filter(function(){
    return true;
});

$puppy->get($uri, $controller)
      ->filter($middleware1)
      ->filter($middleware2)
      ->filter($middleware3);

$puppy->get($uri, $controller)->filter(function(Request $request){
    ...
});

$puppy->group($routes)
    ->filter($middleware1)
    ->filter($middleware2);

$puppy->before(function(Request $request){
    ...
});

$puppy->after(function(Response $response){
    ...
});

$puppy->before($callback1)
      ->before($callback2)
      ->after($callback3)
      ->after($callback4);

$puppy->mirror('mail', 'contact'); //request uri "mail" will point to "contact"

$puppy->mirror('mail/:id', 'contact/{id}');

$puppy->addService('serviceName', function(Container $services){
    return new MyService();
});

$puppy->getService('myService');

$appController->getService('myService');


//you want the request?
$puppy->get('hello', function(Request $request){
    ...
});
    
//you want the request and the config?
$puppy->get('hello', function(Request $request, \ArrayAccess $config){
    ...
});
    
//you want the router and the appController?
$puppy->get('hello', function(Router $router, AppController $appController){
    ...
});

//your module class
class MyModule implements \Puppy\Module\IModule{

    function init(\Puppy\Application $puppy){
        $puppy->get('my-module/:all', function(){
            return 'This is my module';
        });
    }

}

//add the module to the Application
$puppy->addModule(new MyModule());

$puppy->error(function(\Exception $exception){
    ...
});

$puppy->die($controller);