需要 Mojolicious 中的路由帮助
我有带有“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎希望
/
呈现登录表单或个人资料页面。上面的代码将始终将/
显示为登录,因为它首先满足该路由条件,并且永远不会关心您是否通过了身份验证。尝试在
/
的初始路由中进行切换(桥接后的默认路由是不必要的)。关于您的示例的一些注释:
under
而不是bridge
。希望这会有所帮助。
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).A couple of notes on your example:
under
instead ofbridge
.Hope this helps.