需要 Mojolicious 中的路由帮助

发布于 2024-10-14 03:25:17 字数 1362 浏览 0 评论 0原文

我有带有“show”方法的“Pages”控制器和带有“check”方法的“Auths”控制器如果用户通过身份验证,则返回 1 的方法。 我有“默认”页面(“/profile”)。

如果用户经过身份验证,我需要重定向到 / ;如果用户未经身份验证,我需要将所有页面重定向到 / 以及授权表单。我的代码不想正常工作(基于 FastNotes 示例应用程序的身份验证):(

auths#create_form - 带有授权表单的 html 模板。

    $r->route('/')       ->to('auths#create_form')   ->name('auths_create_form');
    $r->route('/login')      ->to('auths#create')    ->name('auths_create');
    $r->route('/logout')     ->to('auths#delete')    ->name('auths_delete');
    $r->route('/signup') ->via('get') ->to('users#create_form')   ->name('users_create_form');
    $r->route('/signup') ->via('post') ->to('users#create')    ->name('users_create');
    #$r->route('/profile') ->via('get') ->to('pages#show', id => 'profile') ->name('pages_profile');

    my $rn = $r->bridge('/')->to('auths#check');
    $rn->route        ->to('pages#show', id => 'profile') ->name('pages_profile');

 $rn->route('/core/:controller/:action/:id')
    ->to(controller => 'pages',
   action  => 'show',
   id   => 'profile')
    ->name('pages_profile');

 # Route to the default page controller
 $r->route('/(*id)')->to('pages#show')->name('pages_show');

I have the "Pages" controller with the "show" method and "Auths" controller with the "check" method which returns 1 if user is authenticated.
I have "default" page ("/profile").

I need to redirect to / if the user is authenticated and redirect all pages to / with the authorization form if the user is not authenticated. My code does not want to work properly (auth based on the FastNotes example application): (

auths#create_form - html-template with the authorization form.

    $r->route('/')       ->to('auths#create_form')   ->name('auths_create_form');
    $r->route('/login')      ->to('auths#create')    ->name('auths_create');
    $r->route('/logout')     ->to('auths#delete')    ->name('auths_delete');
    $r->route('/signup') ->via('get') ->to('users#create_form')   ->name('users_create_form');
    $r->route('/signup') ->via('post') ->to('users#create')    ->name('users_create');
    #$r->route('/profile') ->via('get') ->to('pages#show', id => 'profile') ->name('pages_profile');

    my $rn = $r->bridge('/')->to('auths#check');
    $rn->route        ->to('pages#show', id => 'profile') ->name('pages_profile');

 $rn->route('/core/:controller/:action/:id')
    ->to(controller => 'pages',
   action  => 'show',
   id   => 'profile')
    ->name('pages_profile');

 # Route to the default page controller
 $r->route('/(*id)')->to('pages#show')->name('pages_show');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久而酒知 2024-10-21 03:25:17

您似乎希望 / 呈现登录表单个人资料页面。上面的代码将始终将 / 显示为登录,因为它首先满足该路由条件,并且永远不会关心您是否通过了身份验证。

尝试在 / 的初始路由中进行切换(桥接后的默认路由是不必要的)。

my $r = $self->routes;
$r->get('/' => sub {
    my $self = shift;
    # Check whatever you set during authentication
    my $template = $self->session('user') ? '/profile' : '/login';
    $self->render( template => $template );
});

关于您的示例的一些注释:

  • 如果您使用 Mojolicious::Lite 作为示例,那么帮助调试问题会更容易。
  • 尝试使用 under 而不是 bridge
  • 尝试使用 $r->get(..) 而不是 $r->route(..)->via(..)

希望这会有所帮助。

It seems you want / to render either a login form OR a profile page. The code above will always show / as login because it hits that route condition first and will never care if you're authenticated or not.

Try a switch in your initial route for / (your default route after the bridge is unnecessary).

my $r = $self->routes;
$r->get('/' => sub {
    my $self = shift;
    # Check whatever you set during authentication
    my $template = $self->session('user') ? '/profile' : '/login';
    $self->render( template => $template );
});

A couple of notes on your example:

  • Its much easier to help debug issues if you use Mojolicious::Lite for examples.
  • Try using under instead of bridge.
  • Try using $r->get(..) instead of $r->route(..)->via(..)

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文