dongqie2028 2015-07-23 15:24
浏览 58

在Laravel 5.1中整理控制器

I'm setting up a Laravel 5.1 project and I've been making good progress with it but have run in to something that I'm struggling to figure out.

Basically, I have been creating objects to insert in to the database in my controller methods. This hasn't been too bad because they're usually one-liners.

However, I've run in to a more complex db entry and my controller has become a little muddied. Let me show you:

/**
 * Store a new ticket for the specified project.
 *
 * @param  int  $id
 * @param  TicketsRequest $request
 * @return Response
 */
public function store_ticket($id, TicketsRequest $request)
{
    $user_id = Auth::user()->id;
    $project_id = $id;

    $project_tickets = Ticket::whereProjectId($id);
    $project_ticket_id = $project_tickets->count() + 1;

    $ticket = new Ticket;
    $ticket->user_id = $user_id;
    $ticket->project_id = $project_id;
    $ticket->project_ticket_id = $project_ticket_id;
    $ticket->title = $request->title;

    $ticket->save();

    $ticket_update = new TicketUpdate;

    $ticket_update->user_id = $user_id;
    $ticket_update->ticket_id = $ticket->id;
    $ticket_update->status_id = $request->status_id;
    $ticket_update->priority_id = $request->priority_id;
    $ticket_update->assigned_to = $request->assigned_to;
    $ticket_update->description = $request->description;

    $ticket_update->save();

    return redirect('/projects');
}

So as you can see, I'm creating a ticket which gets saved to the database, then also creating a ticket update which is also saved to the database.

What I'd like to do is extract this code in to 'something' to clean up my controller.

On my travels, I've found that maybe creating repositories might be the way forward. Otherwise I was thinking about some kind of service but I'm not really convinced that that is the way forward.

I have a subscription to Laracasts and found the following video but it's a little outdated and I was sure if this would still be the 'right' way to do this in Laravel 5.1 (I've found that things seem to have a natural home in 5.1 compared to older versions).

https://laracasts.com/lessons/repositories-simplified

Any suggestions/links etc would be great. Thanks!

  • 写回答

2条回答 默认 最新

  • duanjianxu4288 2015-07-23 16:46
    关注

    If you instantiate your objects often/always using the same set of attributes, you could easily extract that code into models constructors, e.g:

    //in your model
    public function __construct($user_id, $ticket_id, $request) {
      $this->user_id = $user_id;
      $this->ticket_id = $ticket_id;
      $this->status_id = $request->status_id;
      $this->priority_id = $request->priority_id;
      $this->assigned_to = $request->assigned_to;
      $this->description = $request->description;
    }
    
    // in your controller
    $ticket_update = new TicketUpdate($user_id, $ticket->id, $request);
    $ticket_update->save();
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改