PHP code example of jupitern / slim3-skeleton

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

    

jupitern / slim3-skeleton example snippets



use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

// simple route example
$app->get('/welcome/{name}', function (Request $request, Response $response, $args) {
	$name = $request->getAttribute('name');
	$response->getBody()->write("Hello, $name");

	return $response;
});


// example route to resolve request to uri '/' to \App\Http\Site\Welcome::index
$app->any('/', function ($request, $response, $args) use($app) {
	return $app->resolveRoute([\App\Http\Welcome::class, "index"], $args);
});


// example calling http://localhost:8080/index.php/test/nuno with the route bellow
// injects the :name param value into the method $name parameter
// Other parameters in the method will be searched in the container by classname or automatically resolved
// in this example the resolveRoute method will create a user instance and inject it in the controller method
$app->any('/test[/{name}]', function ($request, $response, $args) use($app) {
	return $app->resolveRoute([\App\Http\Welcome::class, "method"], $args);
});

namespace App\Http;
use Jupitern\Slim3\App\Http\Controller;

class Welcome extends Controller
{
	public function method($name, \App\Model\User $user)
	{
	    return get_class($user)."<br/>name = {$name}";
	}
}


namespace App\Console;

class Test extends Command
{

	public function method($a, $b='foobar')
	{
		return
			"\nEntered console command with params: \n".
			"a= {$a}\n".
			"b= {$b}\n";
	}
}

// since param "b" is optional you can use one of the following commands

> php cli.php Test method a=foo b=bar

> php cli.php Test method a=foo

$app = \Lib\Framework\App::instance();
// or simpler using a helper function
$app = app();

debug(['a', 'b', 'c']);
// or debug and exit passing true as second param
debug(['a', 'b', 'c'], true);

$user = \App\Model\User::find(1);
echo $user->Name;

/* @var $mail \PHPMailer\PHPMailer\PHPMailer */
$mail = app()->resolve('mail');
$mail->addAddress('[email protected]');
$mail->Subject = "test";
$mail->Body    = "<b>test body</b>";
$mail->AltBody = "alt body";
$mail->send();

$filesystem = app()->resolve('fs_local');
$contents = $filesystem->listContents(STORAGE_PATH, true);
var_dump($contents);

// save user info in session
\Jupitern\Slim3\Utils\Session::set('user', ['id' => '1']);
// get user info from session
$uservar = \Jupitern\Slim3\Utils\Session::get('user');
var_dump($uservar);

/** @var \Jupitern\Slim3\Utils\Redis $cache */
$cache = app()->resolve('redis');
$cache->set("cacheKey", "some test value");
echo $cache->get("cacheKey");