本文介绍了当我的代码拆分为多个文件时,如何使用Dancer2 :: Plugin :: Database?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的所有与Dancer2和数据库连接有关的代码示例都将所有Dancer2代码直接放入附加到各种获取和提交请求的匿名子例程中。



我想以某种方式组织代码,使myServices.pm文件实质上只是通往包含执行内容的其他代码文件的路由器。我可以在MyServices :: Submission模块中成功使用params关键字。但是,在这种情况下,我似乎无法使用Dancer2 :: Plugin :: Database中的数据库关键字。



myServices.pm:

  package myServices; 

使用Dancer2;
使用Dancer2 :: Plugin :: REST;
使用Dancer2 :: Plugin :: Database;
use Data :: Dumper;
使用MyServices :: Submission;

得到‘/ direct’=> sub {
my $ dbh =数据库;
返回成功;
};

获得 /间接 =>子{
MyServices :: Submission :: databaseTest();
};

是;

MyServices / Submission.pm:

  package MyServices :: Submission; 

使用Dancer2;
使用Dancer2 :: Plugin :: REST;
使用Dancer2 :: Plugin :: Database;
use Data :: Dumper;


子数据库测试{
my $ dbh = database;
返回成功;
}

true;

对/ direct的调用返回成功。

对/ direct的调用间接返回错误500-内部服务器错误,并显示消息没有提供设置就无法建立数据库连接!。然后打印出我的设置,包括正确的数据库配置。



我的配置文件必须正确,因为对/ direct的调用成功。



问:


  1. 还有其他人可以复制此行为吗? (确保没有丢失
    明显的东西。)

  2. 有没有一种方法可以成功地在MyServices :: Submission模块中成功使用Dancer2 :: Plugin :: Database,或者
    我是否需要搜索另一个数据库连接解决方​​案以便
    满足我的代码组织的需要?


解决方案

当您在MyServices :: Submission中使用Dancer2; 调用时,:






您可以使用选项表示模块应该扩展一个应用程序而不是创建一个新应用程序:

  package MyServices :: Submission; 

使用Dancer2 appname => MyApp;
使用Dancer2 :: Plugin :: Database;

子数据库测试{
my $ dbh = database;
返回成功;
}

1;

现在,来自主应用程序的配置和引擎将在MyServices :: Submission中可用。






顺便说一句,像这样拆分应用程序是个好主意。如果您对其他技术感兴趣,Dancer用户邮件列表中的某人在。这些建议分为六个部分;有关完整列表,请参见。 p>

All code examples I have seen related to Dancer2 and database connections put all Dancer2 code directly in the anonymous subroutines attached to the various 'get' and 'put' requests.

I would like to organize my code in a way that myServices.pm file is essentially just a router to the other code files that contain the meat of what is executing. I can successfully use the params keyword in the MyServices::Submission module. However, I can't seem to use the database keyword from Dancer2::Plugin::Database in this context.

myServices.pm:

package myServices;

use Dancer2;
use Dancer2::Plugin::REST;
use Dancer2::Plugin::Database;
use Data::Dumper;
use MyServices::Submission;

get '/direct' => sub {
    my $dbh = database;
    return 'success';
};

get '/indirect' => sub {
   MyServices::Submission::databaseTest();
};

true;

MyServices/Submission.pm:

package MyServices::Submission;

use Dancer2;
use Dancer2::Plugin::REST;
use Dancer2::Plugin::Database;
use Data::Dumper;


sub databaseTest{
    my $dbh = database;
    return 'success';
}

true;

The call to /direct returns 'success'.
The call to /indirect returns Error 500 - Internal Server Error with the message "Can't get a database connection without settings supplied!". It then prints out my settings, including the correct database configuration.

My configuration file must be fine, because the call to /direct is a success.

Q's:

  1. Can anyone else replicate this behavior? (Make sure I'm not missingsomething obvious.)
  2. Is there a way to successfully use Dancer2::Plugin::Database in the MyServices::Submission module, ordo I need to search for another db connection solution in order tomeet my code organization needs?
解决方案

When you call use Dancer2; in MyServices::Submission, you're actually creating a separate Dancer2 app:


You can use the appname option when you import Dancer2 to say that your module should extend an app instead of creating a new one:

package MyServices::Submission;

use Dancer2 appname => 'MyApp';
use Dancer2::Plugin::Database;

sub databaseTest {
    my $dbh = database;
    return 'success';
}

1;

Now the configuration and engines from the main app will be available inside MyServices::Submission. You can even add additional routes here.


As an aside, splitting up your application like this is a great idea; if you're interested in other techniques, someone on the Dancer users mailing list wrote up some pretty thorough recommendations on how to organize medium- to large-scale Dancer applications. The recommendations are split into six parts; see here for the full listing.

这篇关于当我的代码拆分为多个文件时,如何使用Dancer2 :: Plugin :: Database?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 12:23