PHP code example of jdanielcmedina / lapa
1. Go to this page and download the library: Download jdanielcmedina/lapa 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' );
jdanielcmedina / lapa example snippets
= new \Lapa\Lapa([
'debug' => true ,
'timezone' => 'UTC'
]);
$app->on('GET /' , function () {
return ['message' => 'Welcome to Lapa!' ];
});
$app->on('GET /users' , function () {
return ['users' => []];
});
$app->on('POST /users' , function () use ($app) {
$data = $app->post();
return ['created' => true , 'data' => $data];
});
$app->on('GET /users/:id' , function () use ($app) {
$id = $app->currentParams['id' ];
return ['user' => ['id' => $id]];
});
$app->group('/api/v1' , function () use ($app) {
$app->on('GET /status' , function () {
return ['status' => 'operational' ];
});
$app->on('GET /health' , function () {
return ['health' => 'ok' ];
});
});
$app->vhost('api.example.com' , function () use ($app) {
$app->on('GET /' , function () {
return ['api' => 'v1' ];
});
});
$config = [
'db' => [
'type' => 'mysql' ,
'host' => 'localhost' ,
'database' => 'test' ,
'username' => 'root' ,
'password' => ''
]
];
$users = $app->db->select('users' , '*' );
$id = $app->db->insert('users' , [
'name' => 'John' ,
'email' => 'john@example.com'
]);
$app->db->update('users' ,
['name' => 'Jane' ],
['id' => 1 ]
);
$app->db->delete('users' , ['id' => 1 ]);
$filename = $app->upload('photo' );
$filename = $app->upload('document' , [
'types' => ['application/pdf' ],
'max_size' => 1024 * 1024
]);
$app->download('file.pdf' );
$app->download('file.pdf' , 'custom-name.pdf' );
$app->storage('cache' )->write('key' , $data);
$data = $app->storage('cache' )->read('key' );
$app->storage('cache' )->delete('key' );
$app->storage('cache' )->clear();
$app->view('home' , [
'title' => 'Welcome' ,
'user' => $user
]);
$app->layout('home' , 'default' , [
'title' => 'Welcome' ,
'user' => $user
]);
$app->partial('header' , ['title' => $title]);
namespace Lapa \Plugins ;
class CacheManager {
private $app;
public function __construct ($app) {
$this ->app = $app;
}
public function set ($key, $value, $ttl = 3600 ) {
$file = $this ->app->storage('cache' ) . '/' . md5($key);
$data = [
'value' => $value,
'expires' => time() + $ttl
];
return file_put_contents($file, serialize($data));
}
public function get ($key) {
$file = $this ->app->storage('cache' ) . '/' . md5($key);
if (!file_exists($file)) return null ;
$data = unserialize(file_get_contents($file));
if ($data['expires' ] < time()) {
unlink($file);
return null ;
}
return $data['value' ];
}
}
$app->cache->set('user.1' , $userData);
$user = $app->cache->get('user.1' );
$app->slug = function ($text) {
return strtolower(preg_replace('/[^a-z0-9]+/i' , '-' , $text));
};
$app->truncate = function ($text, $length = 100 ) {
return strlen($text) > $length
? substr($text, 0 , $length) . '...'
: $text;
};
$slug = $app->slug('Hello World' );
$text = $app->truncate($longText, 50 );
$app->on('GET /users' , function () {
});
$app->on('GET /docs' , function () use ($app) {
return $app->docs();
});
$config = ['debug' => true ];
try {
} catch (\Exception $e) {
$app->debug($e->getMessage(), 500 , $e->getTraceAsString());
}
$app->log('Database connection failed' , 'error' );
$app->log('Cache hit for key: user.1' , 'debug' );
[
'debug' => false ,
'secure' => false ,
'errors' => true ,
'timezone' => 'UTC' ,
'upload' => [
'max_size' => 5242880 ,
'allowed_types' => ['image/jpeg' , 'image/png' , 'application/pdf' ]
],
'cache' => [
'ttl' => 3600
],
'cors' => [
'enabled' => false ,
'origins' => '*' ,
'methods' => 'GET, POST, PUT, DELETE, OPTIONS, PATCH' ,
'headers' => 'Content-Type, Authorization, X-Requested-With' ,
'credentials' => false
],
'mail' => [
'enabled' => false ,
'host' => 'smtp.example.com' ,
'port' => 587 ,
'secure' => 'tls' ,
'auth' => true ,
'username' => '' ,
'password' => '' ,
'from_name' => 'Lapa Framework' ,
'from_email' => 'noreply@example.com'
]
]
$config = [
'cors' => [
'enabled' => true ,
'origins' => ['https://example.com' ],
'methods' => 'GET, POST' ,
'headers' => 'X-Requested-With'
]
];
$config = [
'upload' => [
'max_size' => 1024 * 1024 ,
'allowed_types' => [
'image/jpeg' ,
'image/png' ,
'application/pdf'
],
'sanitize' => true
]
];