The symfony Cookbook

Tạo Link tới Application khác

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


About

You are currently reading "The symfony Cookbook" which is licensed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License license.

Master symfony

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Books on symfony

Learn more about symfony with the official guides.
books.sensiolabs.com

L'audit Qualité par SensioLabs

200 points de contrôle de votre applicatif web.
audit.sensiolabs.com
symfony training
Be trained by symfony experts
Feb 21: Köln (Getting Started with Symfony2 - English)
Feb 27: Köln (Mastering Symfony2 - English)
Mar 05: Köln (Web Development with Symfony2 - Deutsch)
Mar 05: Montreal (Web Development with Symfony2 - English)
Mar 05: Montreal (Getting Started with Symfony2 - English)
and more...

Search


powered by google
You are currently browsing "The symfony Cookbook" in for the 1.2 version - Switch to version: - Switch to language:
Creative Commons License This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.
Translation of this work into another language is explicitly allowed.
This version of symfony is not maintained anymore.
If some of your projects still use this version, consider upgrading as soon as possible.

Một project symfony có thể có một hoặc nhiều application. Các Application chỉ dùng chung model, còn lại là độc lập với nhau. Nhưng đôi khi, bạn cần tạo một link tới frontend application từ backend. Ví dụ với một backend CMS bạn cần sửa một bài viết và sau đó link tới bài viết đó ở frontend để xem kết quả.

Trong bài viết này, tôi sẽ hướng dẫn bạn một cách đơn giản để giải quyết vấn đề trên.

Ngoài việc phân tích file cấu hình và chuyển kết quả thành mảng trong file PHP cache, một vài lớp quản lý cấu hình còn có thể cấu hình trực tiếp (các lớp quản lý cấu hình autoload, database, và routing).

Ví dụ, nếu bạn muốn chuyển một file chứa route được viết dưới dạng YAML thành object route tương ứng, bạn có thể làm như sau:

$config = new sfRoutingConfigHandler();
$routes = $config->evaluate(array('/path/to/routing.yml'));

Trong đoạn code trên, $routes là mảng các object route, tương đương với các route xác định trong file YAML. Phương thức generate() nhận một mảng các tên file YAML và ghép nội dung của tất cả các file đó lại.

Phương thức evaluate() nhận một mảng các file cấu hình. Nếu bạn có một vài plugin xác dịnh route trong file routing.yml, thêm đường dẫn file đó vào mảng và cấu hình sẽ được thêm vào cùng với cấu hình chính.

Nhờ có framework decoupling và phương thức sfPatternRouting::setRoutes(), bạn có thể dễ dàng tạo một object frontend routing từ bất kì PHP script nào:

$routing = new sfPatternRouting(new sfEventDispatcher());
$routing->setRoutes($routes);

Tạo URLs bây giờ đơn giản là gọi phương thức generate() của object routing:

$routing->generate('homepage');
$routing->generate('article', array('id' => $id));

Phương thức generate() có tham sô đầu tiên là tên của route, và tham số thứ 2 là mảng các parameter.

Hãy sử dụng những hiểu biết trên để tạo một frontend URL từ backend application.

Trong lớp backendConfiguration, thêm đoạn code sau:

// apps/backend/config/backendConfiguration.class.php
class backendConfiguration extends sfApplicationConfiguration
{
  protected $frontendRouting = null;
 
  public function generateFrontendUrl($name, $parameters = array())
  {
    return 'http://frontend.example.com'.$this->getFrontendRouting()->generate($name, $parameters);
  }
 
  public function getFrontendRouting()
  {
    if (!$this->frontendRouting)
    {
      $this->frontendRouting = new sfPatternRouting(new sfEventDispatcher());
 
      $config = new sfRoutingConfigHandler();
      $routes = $config->evaluate(array(sfConfig::get('sf_apps_dir').'/frontend/config/routing.yml'));
 
      $this->frontendRouting->setRoutes($routes);
    }
 
    return $this->frontendRouting;
  }
 
  // ...
}

Chú ý rằng phương thức generateFrontendUrl() luôn trả về đường dẫn tuyệt đối nên bạn cần phải cung cấp thông tin về host.

Với đoạn code này, bạn có thể tạo ra một frontend URL ở bất kì đâu trong backend. Dưới đây là ví dụ cách redirect người dùng tới frontend từ một action ở backend:

$this->redirect($this->getContext()->getConfiguration()->generateFrontendUrl('hello', array('name' => 'Bar')));

Bạn cũng có thể tạo một helper cho template:

function link_to_frontend($name, $parameters)
{
  return sfProjectConfiguration::getActive()->generateFrontendUrl($name, $parameters);
}

Đó là tất cả những gì chúng ta phải làm!

Nếu bạn có nhiều application, thật dễ dàng refactor code trên để tạo URL cho các application khác nhau.

Kĩ thuật mô tả trong hướng dẫn này được sử dụng trong task app:routes.

Questions & Feedback

If you find a typo or an error, please register and open a ticket.

If you need support or have a technical question, please post to the official user mailing-list.