asaokamei / slim4-starter
my starter project for Slim4 + PHP-DI .
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 2
Type:project
Requires
- ext-json: *
- aura/session: ^2.1
- filp/whoops: ^2.7
- monolog/monolog: ^2.0
- nyholm/psr7: ^1.3
- nyholm/psr7-server: ^1.0.0
- php-di/php-di: ^6.1
- slim/csrf: ^1.0
- slim/http: ^1.0
- slim/slim: ^4.0
- slim/twig-view: ^3.0
- vlucas/phpdotenv: ^5.2
Requires (Dev)
- phpunit/phpunit: ^8.5
README
A starter project for ordinary web sites, based on slim/slim-skeleton.
Uses,
- Slim4,
- PHP-DI,
- nyholm/psr7,
- monolog,
- Twig (using
slim/twig-view
) - Aura/Session
- filp/whoops
- vlucas/phpdotenv
License
MIT License
Demo
installation.
git clone https://github.com/asaokamei/slim4-starter
cd slim4-starter
composer install
run demo, after installation.
cd public
php -S 127.0.0.1:8000 index.php
AbstractController
App\Controllers\AbstractController
provides useful features,
such as;
- execute class method based on http method.
- bind arguments with input value.
- modify input value.
For instance, the method onPost
is executed when
http method is post.
use App\Controllers\AbstractController; class WelcomeController extends AbstractController { public function action() {} // <- always executed, or public function onMethod() {} // <- executed based on HTTP method }
execution of method.
- if
action
method is present, always executeaction
. - check http method, and execute
on{$method}
. - post with
_method
to specify http method other than 'GET' and 'POST'.
bind argument
For route, /users/{user_id}
, the argument $user_id
is bound with
the route variable, {user_id}
.
class WelcomeController extends AbstractController { public function onGet($user_id) {} }
modify input value
You can modify the input value using a arg{$InputKey}
method in a controller.
- Return a single value to replace the original input value,
as shown in
argTags
. - Or, return an associative array to create a new entry,
as shown in
argUserId
.
// 'users/{user_id}/{tags} class WelcomeController extends AbstractController { public function onGet($user_id) {} private function argUserId($user_id) { return ['user' => $this->user->find($user_id)]; } private function argTags($tags) { return explode(',', $tags); } }
Twig Functions
additional functions for Twig.
csrf_token()
CSRF tokens in hidden tag for Slim-Csrf.
(was hidden_csrf_token()
)
{{ csrf_token() }}
path(string $routeName, array $data = [], array $queryParams = [])
Get the url for a named route
(same as url_for()
in slim/twig-view).
url(string $routeName, array $data = [], array $queryParams = [])
Get the full url for a named route.
(same as full_url_for()
in slim/twig-view).
More Functions from slim/twig-view.
is_current_url(string $routeName, array $data = [])
check if the route name is the current URL.
current_url(bool $withQueryString = false)
Get the current path.
get_uri()
Get Psr\Http\Message\UriInterface
object.
base_path()
Get base path string.