Mojolicious::Plugin::Check - Mojolicious plugin for controller level conditions.
This module provide delayed to around_action hook execution for conditions to use with database, models and over controller level checks. You do not have to use add_condition directly for this because: conditions check route/headers not business logic, you can`t save something in stash, etc.
# Add plugin in startup $self->plugin('Check'); # Good example: $r->add_condition(integer => sub {...}); $app->add_checker(user => sub {...}); $r->get('/user/:id')->over( integer => 'id', # good, simple integer check user => 'id', # good, delay check )->to('foo#bar'); # Bad example: $r->add_condition(integer => sub {...}); $r->add_condition(user => sub {...}); $r->get('/user/:id')->over( integer => 'id', # good, simple integer check user => 'id', # bad, too early for DB, Model, Controller etc. )->to('foo#bar');
Same as add_condition, but delay execution to around_action hook level.
# Simple "true" checker example $app->add_checker('true' => sub { my ($route, $c, $captures, $pattern) = @_; return $captures->{$pattern} ? 1 : 0; }); # You can use database and save objects in stash to use in controllers $app->add_checker('user_exists' => sub { my ($route, $c, $captures, $pattern) = @_; my $id = $captures->{$pattern}; my $db = $c->pg->db; $c->stash->{user} = $db->query('...', $id); return $c->stash->{user} ? 1 : 0; }); # The user is guaranteed to have or render not_found page. $r->get('/user/:id')->over(user_exists => 'id')->to(cb => sub{ my ($c) = @_; my $user = $c->stash('user'); ... });
Return values for sub:
Check pass.
Check fail. Render "Page not found" automatically.
Check fail. You should render something manually.
# Example "true" for forbidden status: $self->add_checker('true' => sub { my ($route, $c, $captures, $pattern) = @_; unless( $captures->{$pattern} ){ $c->render(text => 'Forbidden', status => 403); return undef; } return 1; });
Dmitry E. Oboukhov <unera@debian.org>, Roman V. Nikolaev <rshadow@rambler.ru>
Copyright (C) 2017 Dmitry E. Oboukhov <unera@debian.org> Copyright (C) 2017 Roman V. Nikolaev <rshadow@rambler.ru>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
To install Mojolicious::Plugin::Check, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Check
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Check
For more information on module installation, please visit the detailed CPAN module installation guide.