Accessing Mojolicious helpers from a module using Mojo::Base?

VirtualWolf

I have an existing application (my website) that I'm doing some code tidying in, and the tidy up is following the same sort of idea as the Mojo::Pg example here, with separate model and controller files to keep things defined. My site accesses both Flickr and Last.fm's APIs, and I have a helper defined in Site::Helpers:

$app->helper(
    get_base_rest_url => sub {
        my ( $self, $config ) =  @_;

        sswitch ( $config ) {
            case 'photos': {
                my $base_url = 'https://api.flickr.com/services/rest/';
                my $user_id  = '7281432@N05';
                my $api_key  = $self->app->config->{ 'api_token' }{ 'flickr' };

                my $url =
                    "$base_url"
                    . "?user_id=$user_id"
                    . "&api_key=$api_key"
                    . "&per_page=" . $self->session->{ per_page }
                    . '&format=json'
                    . '&nojsoncallback=1';

                return $url;
            }

            case 'music': {
                my $base_url = 'https://ws.audioscrobbler.com/2.0/';
                my $username = 'virtualwolf';
                my $api_key  = $self->app->config->{ 'api_token' }{ 'last_fm' };
                my $per_page = $self->session->{ 'per_page' };

                my $url = "$base_url?user=$username&limit=$per_page&api_key=$api_key&format=json";

                return $url;
            }
        }
    }
);

The problem I'm running into is that I don't know how to access that helper from the Site::Model::Photos module. The error is

Can't locate object method "get_base_rest_url" via package "Site::Model::Photos"

which is fair enough, but I can't work out how to actually get at that get_base_rest_url helper (or alternatively, how to access the api_token config).

Logioniz

The problem is that your module have not got app attribute/method which get access to your app.

So, when you create instance of Site::Model::Photos you need to pass app to it in param and make it weaken something like that:

package Site::Model::Photos
use Scalar::Util 'weaken';
sub new {
  my $class = shift;
  my $app = shift;
  my $hash = {app => $app, ...};
  weaken $hash->{app};
  return bless $hash, $class;
}

sub your_method {
  my $self = shift;
  $self->{app}->get_base_rest_url(...);
}

1;

Or you may to use this module https://metacpan.org/release/Mojolicious-Plugin-Model which do it for you:

package Site::Model::Photos
use Mojo::Base 'MojoX::Model';

... code of your module ...

sub your_method {
  my $self = shift;
  $self->app->get_base_rest_url(...);
}

1;

And in your App.pm need to add this:

$app->plugin('Model', {namespaces => ['Site::Model']});

And use it that in controller:

$c->model('photos');
$c->app->model('photos');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating and using an Elixir helpers module in Phoenix

From Dev

Accessing NPM module from Node using admin privileges (sudo)

From Dev

Accessing helpers from the parent app in an isolated Rails engine

From Dev

Accessing helpers from the parent app in an isolated Rails engine

From Dev

Can't locate module(s) using Mojo::DOM

From Dev

How to test a maven plugin (Mojo) from a other module

From Dev

Obtain root directory of multi-module maven project from a Mojo

From Dev

Accessing directive from a different module

From Dev

MVC using methods from a helpers namespace

From Dev

Accessing python generators in parallel using multiprocessing module

From Dev

Accessing JavaScript module method using string variable

From Dev

Accessing constants from deep nested module

From Dev

accessing variables of fortran module from c++

From Dev

angularjs - accessing a factory from a controller in another module

From Dev

Directives Accessing Scope From Another Module

From Dev

Accessing a Function from HTML inside of a JS Module

From Dev

Problems Accessing DOM from Browserify Module

From Dev

Base url in Mojolicious template rendering

From Dev

Accessing common part of an union from base class

From Dev

Reflections from Maven Mojo

From Dev

Using params on template helpers from Iron Router in Meteor

From Dev

Magento 1.9 - multiple helpers in module

From Dev

Class and module scope in rails helpers

From Dev

Magento 1.9 - multiple helpers in module

From Dev

RSpec view specs: accessing view helpers

From Dev

Mojolicious - Using external JS files

From Dev

Asynchronous Chat Server using Mojolicious

From Dev

Calling javascript href from mojolicious

From Dev

Accessing local module variables from test benches in Verilog

Related Related

  1. 1

    Creating and using an Elixir helpers module in Phoenix

  2. 2

    Accessing NPM module from Node using admin privileges (sudo)

  3. 3

    Accessing helpers from the parent app in an isolated Rails engine

  4. 4

    Accessing helpers from the parent app in an isolated Rails engine

  5. 5

    Can't locate module(s) using Mojo::DOM

  6. 6

    How to test a maven plugin (Mojo) from a other module

  7. 7

    Obtain root directory of multi-module maven project from a Mojo

  8. 8

    Accessing directive from a different module

  9. 9

    MVC using methods from a helpers namespace

  10. 10

    Accessing python generators in parallel using multiprocessing module

  11. 11

    Accessing JavaScript module method using string variable

  12. 12

    Accessing constants from deep nested module

  13. 13

    accessing variables of fortran module from c++

  14. 14

    angularjs - accessing a factory from a controller in another module

  15. 15

    Directives Accessing Scope From Another Module

  16. 16

    Accessing a Function from HTML inside of a JS Module

  17. 17

    Problems Accessing DOM from Browserify Module

  18. 18

    Base url in Mojolicious template rendering

  19. 19

    Accessing common part of an union from base class

  20. 20

    Reflections from Maven Mojo

  21. 21

    Using params on template helpers from Iron Router in Meteor

  22. 22

    Magento 1.9 - multiple helpers in module

  23. 23

    Class and module scope in rails helpers

  24. 24

    Magento 1.9 - multiple helpers in module

  25. 25

    RSpec view specs: accessing view helpers

  26. 26

    Mojolicious - Using external JS files

  27. 27

    Asynchronous Chat Server using Mojolicious

  28. 28

    Calling javascript href from mojolicious

  29. 29

    Accessing local module variables from test benches in Verilog

HotTag

Archive