trying to catch a clic on a link

73 views
Skip to first unread message

amka...@gmail.com

unread,
Feb 13, 2018, 3:32:01 PM2/13/18
to Mojolicious
Hi,

I am trying to catch a clic on a link, see below (a user clic on "href="/rouage-ch_exemple...")

@@ exemplestarifs.html.ep
...
<a href="/rouage-ch_exemple-1.zip">Exemple 1</a> : module 0.09 mm, sécurité 0.03 mm, nombres de dents : 11 et 53.<br>
<a href="/rouage-ch_exemple-2.zip">Exemple 2</a> : module 0.12 mm, sécurité 0.04 mm, nombres de dents : 11, 11 et 17.<br>
<a href="/rouage-ch_exemple-3.zip">Exemple 3</a> : module 0.15 mm, sécurité 0.05 mm, nombres de dents : 11, 17, 53 et 11.<br>
...

With this code :

get '/rouage-ch_exemple-(:noExemple)' => sub {
  my $c   = shift;
  my $noExemple = $c->param('noExemple');
  log_visite($c,"exemple $noExemple");                       # that should write a line in a postresql table, it's the purpose.
};

But that don't work.

Can please someone help me ?


Stefan Adams

unread,
Feb 13, 2018, 4:54:51 PM2/13/18
to mojolicious
We can't speak to `log_visite`, so why don't you try $c->app->log->info("exemple $noExemple"); and then check the STDERR or log/ directory for the output.

Do you know that the route is even being called?

Are your .zip files actually static files in .../public?  If so, that route won't get hit.  If so, you'll need to use a hook, or not hand them out with Static. 

amka...@gmail.com

unread,
Feb 14, 2018, 3:11:39 AM2/14/18
to Mojolicious
Are your .zip files actually static files in .../public?  If so, that route won't get hit.  If so, you'll need to use a hook, or not hand them out with Static.

My .zip files are in .../public, so, I have to study the hook mojolicious documentation. Thanks  :)

amka...@gmail.com

unread,
Feb 19, 2018, 5:15:21 AM2/19/18
to Mojolicious
Hi,

I tried things like

app->hook(before_routes => sub {
  my $c = shift;
  print "Hi\n"
});

Changing "before_routes" with other things but impossible : I catch all the links except my download link  :(

Stefan Adams

unread,
Feb 19, 2018, 11:29:00 AM2/19/18
to mojolicious
I think you want before_dispatch.

Emitted right before the static file server and router start their work.

amka...@gmail.com

unread,
Feb 19, 2018, 11:51:29 AM2/19/18
to Mojolicious
app->hook(before_dispatch => sub {

  my $c = shift;
  print "Hi\n";
});

don't react for the links (to .../public folder).

Have you please another idea ?

Илья Рассадин

unread,
Feb 19, 2018, 2:31:01 PM2/19/18
to mojol...@googlegroups.com

Do you have any server dispatching requests before Mojolicious?

Nginx or Apache maybe?

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Stefan Adams

unread,
Feb 19, 2018, 6:08:00 PM2/19/18
to mojolicious
On Mon, Feb 19, 2018 at 1:31 PM, Илья Рассадин <elca...@gmail.com> wrote:

Do you have any server dispatching requests before Mojolicious?

Nginx or Apache maybe?


Agreed.  Check out this example:

$ cat /tmp/hook 
use Mojolicious::Lite;

# Use /etc as the public folder root (because everyone has an /etc)
unshift @{app->static->paths}, '/etc';
app->hook(before_dispatch => sub {
  my $c = shift;
  print "Hi\n";
});

app->start;

You can see the hook prints 'Hi' as expected, and retrieves the requested file /issue (under /etc)
$ perl /tmp/hook get /issue
Hi
Ubuntu 16.04.3 LTS \n \l

amka...@gmail.com

unread,
Feb 20, 2018, 2:17:01 PM2/20/18
to Mojolicious
Look please this :

get_link.pl :
--------------------------------------------------------------
#!/usr/bin/env perl
use Mojolicious::Lite;


app->hook(before_dispatch => sub {
  my $c = shift;
  print "Hi\n";
});
get '/page'  => sub { my $c = shift ; $c->render };

app->start;

__DATA__
@@ page.html.ep
<a href="/ex1.zip">Exemple 1</a>
--------------------------------------------------------------

- that print Hi when I load "page"
- print Hi when I do : perl get_link.pl get /ex1.zip
- do NOT print Hi when I click my link "Exemple 1"
- but print Hi if I write <a href="/ex1.txt"> :  txt file and not more zip... That displays the content of the text file in the browser and print Hi.

Impossible to do that with a zip file...  Do please someone have an idea ?

NB : ex1.zip and ex1.txt files are in .../public/






Stefan Adams

unread,
Feb 20, 2018, 8:30:32 PM2/20/18
to mojolicious
$ mkdir public
$ date > public/ex1.txt
$ zip public/ex1.zip public/ex1.txt 
$ ls -l get_link.pl public/
-rw-rw-r-- 1 stefan stefan  240 Feb 20 19:20 get_link.pl
-rw-rw-r-- 1 stefan stefan  29 Feb 20 19:21 public/ex1.txt
-rw-rw-r-- 1 stefan stefan 207 Feb 20 19:21 public/ex1.zip
$ perl get_link.pl get /ex1.txt
Hi
Tue Feb 20 19:21:04 CST 2018
$ perl get_link.pl get /ex1.zip
Hi
PK....gibberish....

All seems like it's working as expected to me.  It sounds as if the only situation it doesn't print Hi is when you click the link.  Can you hover over that link and make sure the link is what you're expecting?  Are you getting a 404 from Mojolicious?  Or what are you getting with the route /ex1.zip?

I also ran this as a daemon and loaded it in my browser and when I clicked the link for Exemple 1 Hi appeared in my STDOUT.

Paolo Saudin

unread,
Feb 21, 2018, 1:11:51 AM2/21/18
to mojol...@googlegroups.com
I confirm is working perfectly here too, windows 10 and Mojolicious (7.61, Doughnut). When link point to zip file download it, when point to txt file xhow it's content
Paolo

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscribe@googlegroups.com.

amka...@gmail.com

unread,
Feb 21, 2018, 10:24:36 AM2/21/18
to Mojolicious
When I click my link to the zip, that download the zip file.

Nothing in the console...

mojolicious@poste1:~/rd/test_perl$ perl get_link.pl daemon -m development
[Wed Feb 21 16:20:36 2018] [info] Listening at "http://*:3000"
Server available at http://127.0.0.1:3000
Hi                                                                       # <---  I load "page"
[Wed Feb 21 16:20:43 2018] [debug] GET "/page"
[Wed Feb 21 16:20:43 2018] [debug] Routing to a callback
[Wed Feb 21 16:20:43 2018] [debug] Rendering template "page.html.ep" from DATA section
[Wed Feb 21 16:20:43 2018] [debug] 200 OK (0.002046s, 488.759/s)
Hi                                                                       # <---  I load "page"
[Wed Feb 21 16:20:45 2018] [debug] GET "/page"
[Wed Feb 21 16:20:45 2018] [debug] Routing to a callback
[Wed Feb 21 16:20:45 2018] [debug] Rendering cached template "page.html.ep" from DATA section
[Wed Feb 21 16:20:45 2018] [debug] 200 OK (0.000676s, 1479.290/s)
                                                                       # <---  I click the link... (nothing here)


:(


Stefan Adams

unread,
Feb 24, 2018, 11:48:57 AM2/24/18
to mojolicious
I got nothing...  Same code, executed the same way...  What version of Mojolicious are you using?

Instead of using `print`, try `warn` or even better use $c->app->log->info(...)

$ perl get_link.pl daemon -m development -l http://*:3002
[Sat Feb 24 10:43:07 2018] [info] Listening at "http://*:3002"
Server available at http://127.0.0.1:3002
Hi                                                                       # <---  I load "/"
[Sat Feb 24 10:43:15 2018] [debug] GET "/"
[Sat Feb 24 10:43:15 2018] [debug] Template "not_found.development.html.ep" not found
[Sat Feb 24 10:43:15 2018] [debug] Template "not_found.html.ep" not found
[Sat Feb 24 10:43:15 2018] [debug] Rendering template "mojo/debug.html.ep"
[Sat Feb 24 10:43:15 2018] [debug] Rendering template "mojo/menubar.html.ep"
[Sat Feb 24 10:43:15 2018] [debug] Your secret passphrase needs to be changed
[Sat Feb 24 10:43:15 2018] [debug] 404 Not Found (0.024707s, 40.474/s)
Hi                                                                       # <---  I load "page"
[Sat Feb 24 10:43:23 2018] [debug] GET "/page"
[Sat Feb 24 10:43:23 2018] [debug] Routing to a callback
[Sat Feb 24 10:43:23 2018] [debug] Rendering template "page.html.ep" from DATA section
[Sat Feb 24 10:43:23 2018] [debug] 200 OK (0.000766s, 1305.483/s)
Hi                                                                       # <---  I click the link for http://localhost:3002/ex1.zip

$ mojo version
CORE
  Perl        (v5.22.1, linux)
  Mojolicious (7.61, Doughnut)

OPTIONAL
  EV 4.0+                 (n/a)
  IO::Socket::Socks 0.64+ (n/a)
  IO::Socket::SSL 1.94+   (2.024)
  Net::DNS::Native 0.15+  (n/a)
  Role::Tiny 2.000001+    (2.000001)

You might want to update your Mojolicious to 7.68!

$ cat get_link.pl 
#!/usr/bin/env perl
use Mojolicious::Lite;

app->hook(before_dispatch => sub {
  my $c = shift;
  print "Hi\n";
});
get '/page'  => sub { my $c = shift ; $c->render };

app->start;

__DATA__
@@ page.html.ep
<a href="/ex1.zip">Exemple 1</a>
Reply all
Reply to author
Forward
0 new messages