小编BDu*_*elz的帖子

如何组织控制器以获得最佳结构(MVC)

什么是组织控制器的最佳方式.假设我有一个用户控制器和一个注册操作,我是否应该在我验证和处理数据的过程中执行process_registration操作,或者只是在注册操作本身内执行所有处理.我是否应对每个需要它的操作进行验证/处理操作(注册,进程注册等等)

我还注意到很多人都有模块和控制器,仅用于验证和处理信息,(我想将所有验证逻辑和规则保存在一个位置?)

我想我的问题是,事情需要分开多远?这个问题也适用于模型和视图.

php model-view-controller code-organization

6
推荐指数
1
解决办法
1290
查看次数

代码设计流程?

我将要开发一个项目,一个Web应用程序.我正在阅读37signals在线获取真正的小册子(http://gettingreal.37signals.com/),我理解建立整个网站的推荐过程.头脑风暴,素描,HTML,代码.

他们轻轻地触及每个过程,但他们从未真正谈论过编码过程(他们所说的只是保持代码精益).我一直在阅读有关它的不同方法(从上到下,从下到上),但我对每种方式都不太了解.我甚至读过某个地方,在实际编写代码之前应该为代码编写测试?什么?

在构建应用程序时应该遵循什么编码过程.

如果有必要,我使用PHP和框架.

process

6
推荐指数
1
解决办法
965
查看次数

PHP中的多层..正确的方法?

我有一个具体的问题,可以使用一个一般性的答案...用PHP构建多层应用程序时,是否必须在业务逻辑层中完成所有工作,或者任何层都可以工作...例如,可以说我正在构建一个在表示层上显示用户信息(来自数据库)的应用程序。我应该使用业务层将数据简单地传递到表示层,还是直接从表示层内的数据库中获取信息。应该仅使用表示层来表示数据,还是使用访问器层来获取数据,并且所有工作都在业务层中完成?

另外,说到不同的层,最好是按程序执行操作,还是使用OOP(例如,使用include来显示模板,使用类来包含模板,通过程序来验证数据还是使用类,或者使用函数vs类来获取数据)数据库中的数据等)

如您所见,我正在尝试了解事物的工作方式以及执行事物的最佳方法。话虽如此,如果您对此主题有任何建议或任何一般性提示,请离开他们。

谢谢

php model-view-controller multi-tier

5
推荐指数
1
解决办法
4010
查看次数

如何在SQLAlchemy中跨多个表进行联合?

我有一些不同的通知表,我想对所有表进行合并以向用户显示所有通知。但是,工会没有正常工作。

Python代码

def _get_notifications_query(self, unconfirmed_only=True):
    '''
    Return base query to return this users notifications.

    @param unconfirmed_only
    @return Query object
    '''        
    requests = (
        DBSession.query(FriendshipRequestNotification)
        .outerjoin(UserFriendshipRequestNotification,
                   UserFriendshipRequestNotification.notification_id==FriendshipRequestNotification.id)
        .filter(UserFriendshipRequestNotification.user_id==self.id))
    confirmations = (
        DBSession.query(FriendshipConfirmationNotification)
        .outerjoin(UserFriendshipConfirmationNotification,
                   UserFriendshipConfirmationNotification.notification_id==FriendshipConfirmationNotification.id)
        .filter(UserFriendshipConfirmationNotification.user_id==self.id))
    comments = (
        DBSession.query(CommentNotification)
        .outerjoin(UserCommentNotification,
                   UserCommentNotification.notification_id==CommentNotification.id)
        .filter(UserCommentNotification.user_id==self.id))

    if unconfirmed_only:
        requests.filter(UserFriendshipRequestNotification.is_confirmed==False)
        confirmations.filter(UserFriendshipConfirmationNotification.is_confirmed==False)
        comments.filter(UserCommentNotification.is_confirmed==False)

    return requests.union(confirmations, comments)
Run Code Online (Sandbox Code Playgroud)

使用:user._get_notifications_query(unconfirmed_only = False).all()

SQL生成

SELECT anon_1.friendship_request_notifications_id AS anon_1_friendship_request_notifications_id, anon_1.friendship_request_notifications_created_at AS anon_1_friendship_request_notifications_created_at, anon_1.friendship_request_notifications_requester_id AS anon_1_friendship_request_notifications_requester_id 
FROM (SELECT friendship_request_notifications.id AS friendship_request_notifications_id, friendship_request_notifications.created_at AS friendship_request_notifications_created_at, friendship_request_notifications.requester_id AS friendship_request_notifications_requester_id 
FROM friendship_request_notifications LEFT OUTER JOIN users_friendship_request_notifications …
Run Code Online (Sandbox Code Playgroud)

python union sqlalchemy

5
推荐指数
2
解决办法
6272
查看次数

有没有一种简单的方法可以在Pyramid中找到运行时环境

在金字塔中,我需要根据不同的运行时环境渲染我的模板 - 启用谷歌分析,使用缩小的代码等(在生产时).有没有一种简单的方法来找出当前的环境 - 也许是一个现有的标志来找出使用哪个ini文件?

python pyramid

5
推荐指数
1
解决办法
1435
查看次数

应该在Cookie(PHP)中保存什么类型的信息

我正在制作登录用户的登录/登出课程,根据用户的选择设置cookie.用户输入他们的电子邮件/密码并检查数据库,电子邮件/密码组合是否存在创建会话,并设置了cookie(用户ID)并且用户被重定向...然后我有一个记录用户的功能通过获取该cookie中保存的用户ID,检查该用户ID是否存在,然后再次将用户数据保存在会话中...我想知道是否有人看到任何有关此事的错误/不安全的事情.

简短的例子,我相信你们可以得到它的要点......

function login($email, $password, $remember){
  // Check the database for email/password combo
  if(/*user exists*/){ // if the user exists
    $_SESSION = /*User data*/ // save the users data in a session
    if($remember){
      setcookie('user_id', /*User id*/); // save the user id in a cookie
    }
    header("location: index.php");// redirect
  }
}

function Check_Cookie(){
  if(isset($_COOKIE['user_id'])){
    return $this->Log_In_ID($_COOKIE['user_id']);
  }else{
    return false
  }
}

function Log_In_ID($id){
  //Check the database if the user id exists
  if(/*user exists*/){ // if the user exists
    $_SESSION = /*User …
Run Code Online (Sandbox Code Playgroud)

php security cookies login

4
推荐指数
1
解决办法
437
查看次数

如何只显示帖子的前x个字符作为预览(PHP)?

我想知道如何只显示帖子的第一个"x"字符作为预览.有点像StackOverflow在显示问题列表时所做的事情.

The quick brown fox jumps over the lazy dog
Run Code Online (Sandbox Code Playgroud)

The quick brown fox jumps...
Run Code Online (Sandbox Code Playgroud)

我不想在中间分开一个字.我认为爆炸功能在每个空间分裂,爆炸("",$ post),但我不确定是否有其他方式.谢谢

php function

4
推荐指数
1
解决办法
1283
查看次数

如何/何时设计界面?

我正在努力养成在开始任何实际编码之前设计我的网站界面的习惯.我已经通过37个信号读取了"Getting Real",他们建议在生成任何实际代码之前先做界面.

究竟是什么意思?这是否意味着使用纯HTML和CSS来设计网站并在之后添加php,js逻辑页面,或者从一开始就可以在php,js中添加?

如果您使用框架,我应该设置简单调用视图的空控制器,或者早期阶段应该只是html,css?

另外,你们先考虑设计与后期的设计有何关系?

编辑我正在谈论我用笔和纸绘制所有内容之后......我只是在编写html模型.而且我不太确定使用额外的工具,我需要学会这样做

html interface

4
推荐指数
1
解决办法
237
查看次数

存储聚合数据是否违反数据库规范化?

在像SO这样的网站上,我确信绝对有必要存储尽可能多的聚合数据,以避免在每个页面加载时执行所有那些复杂的查询/计算.例如,存储每个问题/答案的投票计数的运行记录,或存储每个问题的答案数,或者查看问题的次数,以便不需要经常执行这些查询.

但这样做是否违反了数据库规范化或任何其他标准/最佳实践?这样做的最佳方法是什么,例如,每个表是否都有另一个聚合数据表,如果它存储在它所代表的同一个表中,何时应该更新聚合数据?

谢谢

database normalization aggregation

3
推荐指数
2
解决办法
1001
查看次数

我应该重构这段代码吗?

该代码用于视图辩论页面.该代码应该确定是否向查看用户显示添加回复表单.

如果用户已登录,并且用户不是辩论的创建者,则检查用户是否已经回复辩论.

如果用户尚未回复辩论,则显示表单...否则,检查用户是否要通过查找回复ID的URL来编辑他们已存在的回复

如果这些测试中的任何一个没有通过,那么我将原因保存为int并将其传递给视图中的switch语句.

逻辑似乎很容易,但我的代码似乎有点草率.

这是代码..(使用Kohana V2.3.4)

public function view($id = 0)
{
    $debate = ORM::factory('debate')->with('user')->with('category')->find($id);

    if ($debate->loaded == FALSE)
    {
        url::redirect();
    }

    // series of tests to show an add reply form
    if ($this->logged_in)
    {
        // is the viewer the creator?
        if ($this->user->id != $debate->user->id)
        {
            // has the user already replied?
            if (ORM::factory('reply')
                ->where(array('debate_id' => $id, 'user_id' => $this->user->id))
                ->count_all() == 0)
            {
                $form = $errors = array
                (
                    'body'      => '',
                    'choice_id' => '',
                    'add'       => '' …
Run Code Online (Sandbox Code Playgroud)

php refactoring kohana

2
推荐指数
2
解决办法
399
查看次数