Mẫu php hồ sơ người dùng

Hôm nay qua chúng ta đã học rất nhiều thứ. Với rất ít dòng mã PHP, trình tạo quản trị viên symfony cho phép lập trình viên tạo giao diện phụ trợ trong vài phút

hôm nay, chúng ta sẽ khám phá cách symfony quản lý dữ liệu liên tục giữa các yêu cầu HTTP. Như bạn đã biết, giao thức HTTP là không trạng thái, có nghĩa là mỗi yêu cầu không phụ thuộc vào yêu cầu trước hoặc hiện tại. Các trang web ngày nay cần có cách lưu lại dữ liệu giữa các yêu cầu để nâng cao khả năng tương tác với người dùng

Một phiên người dùng có thể xác định thông tin xác định thông qua cookie. Trong symfony, lập trình viên không cần trực tiếp quản lý phiên, mà có thể sử dụng các đối tượng

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
3

Người dùng nhấp nháy

Tại quản trị, chúng tôi đã sử dụng đối tượng người dùng với đèn flash. Một đèn flash là một thông báo nhanh được chứa trong phiên người dùng, sẽ tự động xóa đi theo yêu cầu tiếp theo. Nó rất hữu ích khi bạn muốn hiển thị một thông báo tới người dùng sau khi chuyển hướng. Trình tạo quản trị sử dụng flash rất nhiều để hiển thị phản hồi cho người dùng mỗi khi công việc được lưu, xóa, hoặc gia hạn

Một đèn flash được đặt thông qua phương thức

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
4 của lớp
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
3

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}

Tham số đầu tiên xác định kiểu flash và tham số thứ 2 là nội dung thông báo. Bạn có thể sử dụng bất kỳ loại flash nào, nhưng

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
6 và
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
7 là 2 loại phổ biến [chúng được sử dụng bởi trình tạo quản trị viên]

Ta sẽ thêm một tin nhắn flash vào các mẫu. With Jobeet, them ta add into

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
8

// apps/frontend/templates/layout.php

  
 

Trong mẫu, ta sử dụng biến

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
9 để truy cập

Ghi chú

Một vài đối tượng của symfony có thể truy cập từ mẫu, không cần thông qua hành động.

// apps/frontend/modules/job/actions/actions.class.php
class jobActions extends sfActions
{
  public function executeShow[sfWebRequest $request]
  {
    $this->job = $this->getRoute[]->getObject[];
 
    $this->getUser[]->addJobToHistory[$this->job];
  }
 
  // ...
}
 
// apps/frontend/lib/myUser.class.php
class myUser extends sfBasicSecurityUser
{
  public function addJobToHistory[JobeetJob $job]
  {
    $ids = $this->getAttribute['job_history', array[]];
 
    if [!in_array[$job->getId[], $ids]]
    {
      array_unshift[$ids, $job->getId[]];
 
      $this->setAttribute['job_history', array_slice[$ids, 0, 3]];
    }
  }
}
0,
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
9, và
// apps/frontend/modules/job/actions/actions.class.php
class jobActions extends sfActions
{
  public function executeShow[sfWebRequest $request]
  {
    $this->job = $this->getRoute[]->getObject[];
 
    $this->getUser[]->addJobToHistory[$this->job];
  }
 
  // ...
}
 
// apps/frontend/lib/myUser.class.php
class myUser extends sfBasicSecurityUser
{
  public function addJobToHistory[JobeetJob $job]
  {
    $ids = $this->getAttribute['job_history', array[]];
 
    if [!in_array[$job->getId[], $ids]]
    {
      array_unshift[$ids, $job->getId[]];
 
      $this->setAttribute['job_history', array_slice[$ids, 0, 3]];
    }
  }
}
2

Thuộc tính người dùng

Trong 2 ngày chúng tôi đã quên không có yêu cầu chứa một số thứ trong phiên người dùng. Ta will add a new request. để dễ dàng xem các công việc, 3 công việc người dùng xem lần cuối sẽ được hiển thị trên menu với liên kết đến trang công việc đó

Khi người dùng xem một công việc, đối tượng công việc hiện tại cần được thêm vào danh sách công việc đã xem và chứa trong phiên

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
2

Ghi chú

Chúng ta hoàn toàn có thể chứa các đối tượng trực tiếp

// apps/frontend/modules/job/actions/actions.class.php
class jobActions extends sfActions
{
  public function executeShow[sfWebRequest $request]
  {
    $this->job = $this->getRoute[]->getObject[];
 
    $this->getUser[]->addJobToHistory[$this->job];
  }
 
  // ...
}
 
// apps/frontend/lib/myUser.class.php
class myUser extends sfBasicSecurityUser
{
  public function addJobToHistory[JobeetJob $job]
  {
    $ids = $this->getAttribute['job_history', array[]];
 
    if [!in_array[$job->getId[], $ids]]
    {
      array_unshift[$ids, $job->getId[]];
 
      $this->setAttribute['job_history', array_slice[$ids, 0, 3]];
    }
  }
}
3 trong phiên. Nhưng điều này là không nên bởi nếu đối tượng bị thay đổi thì thông tin chứa trong phiên sẽ không còn giá trị

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
29,
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
0

Phương thức

// apps/frontend/modules/job/actions/actions.class.php
class jobActions extends sfActions
{
  public function executeShow[sfWebRequest $request]
  {
    $this->job = $this->getRoute[]->getObject[];
 
    $this->getUser[]->addJobToHistory[$this->job];
  }
 
  // ...
}
 
// apps/frontend/lib/myUser.class.php
class myUser extends sfBasicSecurityUser
{
  public function addJobToHistory[JobeetJob $job]
  {
    $ids = $this->getAttribute['job_history', array[]];
 
    if [!in_array[$job->getId[], $ids]]
    {
      array_unshift[$ids, $job->getId[]];
 
      $this->setAttribute['job_history', array_slice[$ids, 0, 3]];
    }
  }
}
6 lấy các giá trị từ phiên người dùng. Ngược lại, phương thức
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
0 chứa các biến PHP vào phiên

Phương thức

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
29 cũng có một tùy chọn chứa giá trị mặc định nếu không lấy được giá trị trong phiên

Ghi chú

default value được tạo bởi phương thức

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
29 tương đương

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}

Lớp
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
1

Để thuận tiện cho việc tổ chức mã, hãy chuyển mã vào lớp

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
1 lớp. Lớp
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
1 ghi đè lớp mặc định
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
3 với các hành vi riêng cho ứng dụng

// apps/frontend/modules/job/actions/actions.class.php
class jobActions extends sfActions
{
  public function executeShow[sfWebRequest $request]
  {
    $this->job = $this->getRoute[]->getObject[];
 
    $this->getUser[]->addJobToHistory[$this->job];
  }
 
  // ...
}
 
// apps/frontend/lib/myUser.class.php
class myUser extends sfBasicSecurityUser
{
  public function addJobToHistory[JobeetJob $job]
  {
    $ids = $this->getAttribute['job_history', array[]];
 
    if [!in_array[$job->getId[], $ids]]
    {
      array_unshift[$ids, $job->getId[]];
 
      $this->setAttribute['job_history', array_slice[$ids, 0, 3]];
    }
  }
}

Mã đã được sửa lại một chút cho phù hợp với yêu cầu

  • if [!$value = $this->getAttribute['job_history']]
    {
      $value = array[];
    }
    64. một công việc không thể được bao gồm 2 lần

  • if [!$value = $this->getAttribute['job_history']]
    {
      $value = array[];
    }
    65. chỉ chứa 3 công việc được xem gần nhất

Trong bố cục, thêm mã đoạn sau vào trước đoạn biến

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
66

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
6

Bố cục sử dụng một phương thức mới

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
67 để nhận lịch sử công việc hiện tại

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
1

Phương thức

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
67 sử dụng phương thức Propel
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
69 để truy xuất một số đối tượng
// apps/frontend/modules/job/actions/actions.class.php
class jobActions extends sfActions
{
  public function executeShow[sfWebRequest $request]
  {
    $this->job = $this->getRoute[]->getObject[];
 
    $this->getUser[]->addJobToHistory[$this->job];
  }
 
  // ...
}
 
// apps/frontend/lib/myUser.class.php
class myUser extends sfBasicSecurityUser
{
  public function addJobToHistory[JobeetJob $job]
  {
    $ids = $this->getAttribute['job_history', array[]];
 
    if [!in_array[$job->getId[], $ids]]
    {
      array_unshift[$ids, $job->getId[]];
 
      $this->setAttribute['job_history', array_slice[$ids, 0, 3]];
    }
  }
}
3 trong một cuộc gọi

Để giao diện dễ nhìn, chúng ta cần thêm một vài css vào cuối tệp

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
11

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
6

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
2

Để hoàn thiện API lịch sử công việc, ta cần thêm một phương thức để xóa lịch sử

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
8

Thuộc tính người dùng được quản lý bởi một đối tượng của lớp

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
2. Phương thức
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
29 và
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
0 là cách viết tắt của
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
16 và
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
17. Do phương thức
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
18 không có cách viết khác nào trong
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
3, nên chúng ta cần sử dụng chủ sở hữu đối tượng trực tiếp

Ghi chú

Lớp

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
2 cũng được sử dụng bởi
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
61 để chứa các tham số

Ứng dụng bảo mật

Xác thực người dùng

Giống như nhiều tính năng khác trong symfony, vấn đề xác thực cũng được quản lý bởi tệp YAML

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
62. Bạn có thể xem cấu hình mặc định cho ứng dụng phụ trợ trong thư mục
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
63

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
0

Nếu bạn chuyển

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
64 thành
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
65, toàn bộ ứng dụng phụ trợ sẽ yêu cầu người dùng phải đăng nhập để sử dụng

tiền boa

Trong tệp YAML, một giá trị boolean được xác định bằng chuỗi

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
66 và
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
67 hoặc
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
65 và
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
69

Nếu bạn xem nhật ký nội dung trên thanh công cụ gỡ lỗi web, bạn sẽ thấy rằng phương thức

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
80 của lớp
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
81 được gọi mỗi khi bạn truy cập vào trang yêu cầu đăng nhập

Khi người dùng chưa đăng nhập truy cập vào một trang yêu cầu đăng nhập, symfony sẽ chuyển người dùng sang trang

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
82 đã được xác định trong
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
83

// apps/frontend/templates/layout.php

  
 
0

Ghi chú

Unable to setting secure for action login to escape đệ quy

tiền boa

Như chúng ta đã biết trong vòng 4 ngày, các cấu hình tệp giống nhau có thể ở nhiều nơi. Tệp

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
62 cũng vậy. Để bảo mật hoặc bỏ bảo mật cho một hành động hoặc toàn bộ mô-đun, hãy tạo tệp
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
62 trong thư mục
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
63 của mô-đun đó

// apps/frontend/templates/layout.php

  
 
1

Default, layer

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
1 kế thừa từ
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
88, chứ không phải từ
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
3.
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
88 cung cấp thêm các phương thức để quản lý xác thực và ủy quyền người dùng

Để quản lý xác thực người dùng, hãy sử dụng phương thức

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
01 và
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
02

// apps/frontend/templates/layout.php

  
 
2

Partition

Khi một người dùng đã đăng nhập, việc truy cập một số hành động có thể bị hạn chế hoặc không có tùy chọn theo giới hạn quyền của họ. Người dùng cần có quyền hạn để truy cập một trang

// apps/frontend/templates/layout.php

  
 
3

Hệ thống phân quyền của symfony đơn giản mà mạnh mẽ. Bạn có thể xác định quyền truy cập bất kỳ phần nào của ứng dụng

thanh bên

Partition permission

Mục

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
03 trong
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
62 Hỗ trợ toán tử Boolean để mô tả các yêu cầu phân quyền phức tạp

Nếu một người dùng cần phải có cả quyền A và B, thì ta sử dụng dấu trích dẫn

// apps/frontend/templates/layout.php

  
 
4

Nếu người dùng cần có quyền A hoặc B, ta sử dụng 2 dấu ngoặc kép

// apps/frontend/templates/layout.php

  
 
5

Bạn có thể sử dụng nhiều dấu trích dẫn để mô tả bất kỳ biểu thức Boolean nào với bất kỳ loại phân quyền nào

Để quản lý thông tin đăng nhập của người dùng,

// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
88 cung cấp một số phương thức

// apps/frontend/templates/layout.php

  
 
6

Với chương trình phụ trợ Jobeet, chúng tôi không sử dụng thông tin xác thực bất kỳ nào do chúng tôi chỉ có một hồ sơ. người quản lý

bổ sung

Chúng ta không nên làm lại cái bánh xe, chúng ta sẽ không phát triển hành động đăng nhập từ đầu. Thay vào đó, chúng ta sẽ cài đặt một plugin symfony

Một trong những điểm mạnh của symfony framework là plugin hệ thống. Như chúng ta sẽ thấy trong vài ngày sau, việc tạo plugin là một việc dễ dàng. Nó thực sự mạnh mẽ, do một plugin có thể chứa bất kỳ thứ gì từ cấu hình đến các mô-đun và tài sản

hôm nay, chúng ta sẽ cài đặt

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
06 để bảo mật cho ứng dụng phụ trợ

// apps/frontend/templates/layout.php

  
 
7

Lệnh

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
07 cài đặt một plugin thông qua tên của nó. Tất cả các plugin đều nằm trong thư mục
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
08 và mỗi plugin là một thư mục có tên là tên plugin

Ghi chú

Bạn cần cài đặt PEAR để có thể chạy lệnh

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
07

Khi bạn cài đặt plugin sử dụng lệnh

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
07, symfony sẽ cài đặt phiên bản mới nhất của plugin đó. Để cài đặt cụ thể một phiên bản nào đó, vui lòng cung cấp phiên bản thông qua tùy chọn
// apps/frontend/templates/layout.php

  
 
01

Trang plugin liệt kê tất cả các phiên bản có sẵn được nhóm theo phiên bản symfony

Vì plugin nằm trong một thư mục độc lập, bạn cũng có thể tải xuống gói này từ trang web của symfony và giải nén nó, hoặc tạo một liên kết

// apps/frontend/templates/layout.php

  
 
02 tới kho lưu trữ Subversion của nó

Bảo mật phụ trợ

Mỗi plugin có một tệp README mô tả cách cấu hình cho plugin đó

Vui lòng xem cách cấu hình cho plugin vừa cài đặt. Do plugin cần một số lớp mô hình mới để quản lý người dùng, nhóm và quyền, bạn cần xây dựng lại mô hình

// apps/frontend/templates/layout.php

  
 
8

tiền boa

Lệnh

// apps/frontend/templates/layout.php

  
 
03 sẽ xóa tất cả các bảng đã có trước khi tạo lại chúng. Để tránh điều này, bạn có thể xây dựng các mô hình, biểu mẫu và bộ lọc, sau đó, tạo thêm các bảng mới bằng cách thực thi câu SQL sinh ra trong
// apps/frontend/templates/layout.php

  
 
04

Đồng thời, khi các lớp mới được tạo, bạn cần xóa bộ đệm

// apps/frontend/templates/layout.php

  
 
9

Do

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
06 thêm một vài phương thức cho lớp người dùng, nếu bạn cần đổi lớp cha của
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
1 thành
// apps/frontend/templates/layout.php

  
 
07

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
20

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
06 cung cấp một hành động mặc định để xác thực người dùng

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
21

Plugin có thể được sử dụng ở tất cả các ứng dụng của dự án, nên bạn cần chỉ rõ những mô-đun nào bạn muốn sử dụng thông qua cài đặt

// apps/frontend/templates/layout.php

  
 
09

Cuối cùng đã tạo một tài khoản quản trị viên

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
22

tiền boa

Plugin này cung cấp các lệnh để quản lý người dùng, nhóm và quyền từ dòng lệnh. Sử dụng lệnh

// apps/frontend/templates/layout.php

  
 
10 để xem danh sách tất cả các lệnh của không gian tên
// apps/frontend/templates/layout.php

  
 
11

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
23

Khi người dùng không được xác thực, ta cần ẩn đi thanh menu

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
24

Khi người dùng được xác thực, chúng ta cần thêm 1 liên kết để đăng xuất

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
25

tiền boa

Để xem tất cả các route, hãy sử dụng lệnh

// apps/frontend/templates/layout.php

  
 
12

Để Jobeet backend thuận tiện hơn, ta cần một mô-đun để quản lý người dùng quản trị viên. May thay, plugin đã cung cấp sẵn cho ta một module như vậy. Đó là mô-đun

// apps/frontend/templates/layout.php

  
 
13, và bạn cần kích hoạt nó trong
// apps/frontend/modules/job/actions/actions.class.php
public function executeExtend[sfWebRequest $request]
{
  $request->checkCSRFProtection[];
 
  $job = $this->getRoute[]->getObject[];
  $this->forward404Unless[$job->extend[]];
 
  $this->getUser[]->setFlash['notice', sprintf['Your job validity has been extend until %s.', $job->getExpiresAt['m/d/Y']]];
 
  $this->redirect[$this->generateUrl['job_show_user', $job]];
}
83

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
26

Thêm liên kết vào menu

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
27

We are doing done

Kiểm tra người dùng

Hướng dẫn ngày hôm nay chưa kết thúc, chúng ta chưa có người dùng thử nghiệm. Do trình duyệt symfony có thể tạo cookie giả, nên việc kiểm tra hành vi người dùng thực sự đơn giản bằng cách sử dụng trình kiểm tra có sẵn

// apps/frontend/templates/layout.php

  
 
15

Thêm chức năng kiểm tra cho menu tính năng chúng tôi đã tạo hôm nay. Thêm mã đoạn sau vào cuối bài kiểm tra chức năng của mô-đun

// apps/frontend/templates/layout.php

  
 
16

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
28

Để thuận tiện cho việc kiểm tra, đầu tiên chúng ta cần tải lại dữ liệu đồ đạc và khởi động lại trình duyệt để bắt đầu với một phiên làm việc mới

Phương thức

// apps/frontend/templates/layout.php

  
 
17 kiểm tra thuộc tính người dùng

Ghi chú

Người thử nghiệm

// apps/frontend/templates/layout.php

  
 
15 cũng cung cấp phương thức
if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
01 và
// apps/frontend/templates/layout.php

  
 
20 để kiểm tra xác thực người dùng và tự động hóa

Hẹn gặp lại ngày mai

Lớp người dùng symfony là cách tốt để quét đối tượng hóa việc quản lý phiên PHP. Cùng với hệ thống plugin tuyệt vời của symfony và plugin

if [!$value = $this->getAttribute['job_history']]
{
  $value = array[];
}
06 , chúng ta có thể bảo mật cho Jobeet backend trong vài phút. Và chúng ta cũng có thể quản lý các quản trị viên tài khoản, nhờ mô-đun được cung cấp bởi plugin

Ngày mai là ngày cuối cùng của tuần 2, và chúng ta hi vọng sẽ thu được nhiều điều bổ ích

Trang trước Ngày 12. Trình tạo quản trị viên

Trang tiếp theo Ngày 14. Nghỉ giải lao

Tác phẩm này được cấp phép theo Creative Commons Ghi công-Chia sẻ tương tự 3. 0 Giấy phép chưa chuyển đổi giấy phép

Chủ Đề