Skip to content

sharifulin/mojolicious-plugin-utilhelpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

NAME

Mojolicious::Plugin::UtilHelpers - some util helpers for web development

SYNOPSIS

# Mojolicious
$self->plugin('util_helpers');

# Mojolicious::Lite
plugin 'util_helpers';

DESCRIPTION

Mojolicous::Plugin::UtilHelpers is a plugin contains util helpers.

METHODS

Mojolicious::Plugin::UtilHelpers inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

$plugin->register;

Register plugin hooks in Mojolicious application.

common

Common helpers.

  • db

    $self->db

    DB helper, fast access to the dbh of application.

  • conf

    $self->conf('server')->{www}
    
    # tmpl
    %= conf('server')->{www}

    Conf helper, fast access to the conf of application.

  • vu

    $self->vu(0)
    
    # tmpl
    %= vu(0)

    Parts of URL path or vitrual URI.

http

HTTP helpers.

  • is_iphone

    $self->is_iphone

    Check User-Agent headers, returns true if ua =~ iPhone or iPod or iPad or CFNetworks.

  • is_mobile_device

    $self->is_mobile_device

    Check User-Agent headers, returns true if ua =~ iPhone or iPod or iPad or Android or BlackBerry or Palm or Mobile.

  • is_mobile

    $self->is_mobile

    Check User-Agent headers, same as is_mobile_device without iPad.

  • ip

    $self->ip

    Helper returns ip of client, check X-Forwarded-For header, than X-Real-IP header and at last remote address of client.

  • status

    $self->status(404)

    Helper generates a response with any code without body.

  • redirect

    $self->redirect( $url )

    302 redirect, uses return_url helper unless $url.

  • return_url

    $self->return_url( $default_url )

    Helper can return previlous url (same as referer url, exclude login or logout or enter URL). If referer url is empty, return_url returns $default_url or root url '/'.

  • redirect_accel

    $self->redirect_accel( $url, $content_type )

    Helper generates redirect with X-Accel-Redirect and Content-Type headers.

  • xhr_redirect

    $self->xhr_redirect( { ok => 1 }, $redirect_url )

    Helper renders JSON (if request has X-HTTP-Request header) or return redirect helper.

  • get_cookie

    $self->get_cookie( $name )

    Helper returns one value of cookie $name.

util

Util helpers.

  • check_error

    # tmpl
    %= check_error 'field_name'

    Check stash error, if exists any errors, helper loads 'etc/validate' template and returns value of key 'field_name' in stash 'validate' hash.

  • list_splice

    # tmpl
    % my $new = list_slice $list, $count;

    Helper generates array of array (size is $count).

  • perl2json

    $self->perl2json( $data )

    Helper encodes perl structure to JSON.

  • cat

    $self->cat('about/info')

    Helper includes template file (format is html, handler is ep) without cache template.

  • meta

    $self->meta

    Helper uses meta from conf and stash 'meta' if exists and generate new stash 'meta'.

  • page_param

    $self->page_param( $page )

    Helper generates url, replace old page param.

text

Text and string helpers.

  • format_digital

    %= format_digital 123456

    Helper returns 123 456. Second parameters is separator, default value is space.

  • shorty

    %= shorty $str, $length

    Helper returns short string, $length is length of short string, default value is 20.

  • shorty_fix

    %== shorty_fix $str, $length

    Helper same as shorty, but tries fix bad tags.

  • link_title

    %= link_title $link

    Helper returns ucfirst name of domain.

  • text_url

    %== text_url $text

    Helper returns $text, where links are real href (<a href="$link" target="_blank">$link</a>).

  • paragraph

    %== paragraph $info

    Helper returns text with paragraph (<p />).

format

New formats for a Mojolicious application.

  • min

    Type is text/html. After static dispatch set stash 'format' as 'min' if request has min parameters.

    Template name is name.min.ep.

route

Add new shortcut to the routes of application.

  • crud

    Create-read-update-delete routes for admin item.

    $r->crud(blog => 'admin-blog');

    There are a lot of routes:

    GET  /blog -> admin-blog#list (name is admin_blog)
    POST /blog -> admin-blog#listing
    
    GET  /blog/sort -> admin-blog#sort (admin_blog_sort)
    POST /blog/sort -> admin-blog#sorting
    
    GET  /blog/add  -> admin-blog#form, stash 'add' => 1 (admin_blog_add)
    POST /blog/add  -> admin-blog#add
    
    bridge /blog/:id, id => qr/\d+/ -> admin-blog#check
    
    GET  /blog/:id -> admin-blog#item (admin_blog_item)
    
    GET  /blog/:id/edit -> admin-blog#form (admin_blog_edit)
    POST /blog/:id/edit -> admin-blog#edit
    
    GET  /blog/:id/remove -> admin-blog#remove (admin_blog_remove)
    
    GET  /blog/:id/:filter -> admin-blog#list (admin_blog)

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicious.org.

AUTHOR

Anatoly Sharifulin <sharifulin@gmail.com>

BUGS

Please report any bugs or feature requests to bug-mojolicious-plugin-UtilHelpers at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.htMail?Queue=Mojolicious-Plugin-UtilHelpers. We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.

COPYRIGHT & LICENSE

Copyright (C) 2010-2011 by Anatoly Sharifulin.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

About

A lot of util helpers for a Mojolicious apps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages