PHP code example of matiasnamendola / slimpower-slim
1. Go to this page and download the library: Download matiasnamendola/slimpower-slim 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/ */
matiasnamendola / slimpower-slim example snippets
$app = new \SlimPower\Slim\Slim();
$app->get('/hello:name', 'App\IndexController:home');
= new \SlimPower\Slim\Slim();
$app->container->singleton('App\IndexController', function ($container) {
// Retrieve any ler:index');
namespace App;
class IndexController {
// Optional properties.
protected $app;
protected $request;
protected $response;
public function index() {
echo "This is the home page";
}
public function hello($name) {
echo "Hello, $name";
}
// Optional setters.
public function setApp($app) {
$this->app = $app;
}
public function setRequest($request) {
$this->request = $request;
}
public function setResponse($response) {
$this->response = $response;
}
// Init
public function init() {
// Do things now that app, request and response are set.
}
}
$app = new \SlimPower\Slim\Slim();
$app->view(new \SlimPower\Slim\Middleware\Json\JsonView());
$app->add(new \SlimPower\Slim\Middleware\Json\JsonMiddleware());
$app->get('/', function() use ($app) {
$app->render(200, array(
'msg' => 'welcome to my API!',
));
});
$app->get('/user/:id', function($id) use ($app) {
// Your code here.
$app->render(404, array(
'error' => TRUE,
'msg' => 'user not found',
));
});
$app->get('/user/:id', function($id) use ($app) {
// Your code here.
if(...) {
throw new \Exception("Something wrong with your request!");
}
});
$app = new \SlimPower\Slim\Slim();
$app->view(new \SlimPower\Slim\Middleware\Json\JsonView("resource", "meta"));
$app->add(new \SlimPower\Slim\Middleware\Json\JsonMiddleware());
function jsonResponse(){
$app = \SlimPower\Slim\Slim::getInstance();
$app->view(new \SlimPower\Slim\Middleware\Json\JsonView());
$app->add(new \SlimPower\Slim\Middleware\Json\JsonMiddleware());
}
$app->get('/home',function() use($app){
// Regular HTML response.
$app->render("template.tpl");
});
$app->get('/api','jsonResponse',function() use($app){
// This request will have full JSON responses.
$app->render(200, array(
'msg' => 'welcome to my API!',
));
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.