Kohana 3.2.2中使用css、Javascript和Images的示例


Example of using css,Javascript and Images in Kohana 3.2.2

有人能帮我们举一个如何在Kohana 3.2.2 中使用外部文件(css、Javascript和图像)的例子吗

以下是我所做的:在/classes/controller/hello.php

    <?php defined('SYSPATH') OR die('No Direct Script Access');

     Class Controller_Hello extends Controller_Template
     {
      public $template = 'site'; // Default template

       public function action_index()
       {
         $this->template->styles = array('media/css/style.css'=>'screen');
         //$this->template->scripts = array('assets/js/jqtest.js');

       }
      public function before()
       {
        parent::before();
        if($this->auto_render)
         {
         // Initialize empty values
          $this->template->styles = array();
          $this->template->scripts = array();
         }
       }
       /**
        * Fill in default values for our properties before rendering the output.
       */
     public function after()
     {
      if($this->auto_render)
      {
      // Define defaults
       $styles = array('media/css/style.css' => 'screen');
       //$scripts = array(‘http://ajax.googleapis.com/ajax/libs/jquery/1.3.2.js');
      // Add defaults to template variables.
         $this->template->styles=array_reverse(array_merge
         ($this->template->styles,$styles));

       //$this->template->scripts =array_reverse(array_merge
        ($this->template->scripts, $scripts));
         }
       // Run anything that needs to run after this.
       parent::after();
     }
 }

然后在你看来,你只需要使用这个/application/views/site.php

   <head>
       <?php 
       foreach($styles as $file => $type)
       { 
       echo HTML::style($file, array('media' => $type)), ""; 
   }    
   ?>    
  </head>

将图像添加到视图

    <li class="social"><?php echo html::image('media/images/phone.png');?>

取决于是否要通过控制器运行它们。如果您想使用HMVC加载媒体文件,请使用示例控制器。

/classes/controller/media.php

class Controller_Media extends Controller {
    protected $config = NULL;
    public function before()
    {
        parent::before();
        $this->config = Kohana::$config->load('media');
    }
    public function action_css()
    {
        $this->handle_request(
            'style',
            $this->request->param('path'),
            $this->config['styles']['extension']
        );
    }
    public function action_js()
    {
        $this->handle_request(
            'script',
            $this->request->param('path'),
            $this->config['scripts']['extension']
        );
    }
    public function action_img()
    {
        $image = $this->config['images']['directory'].$this->request->param('path');
        $extension = $this->find_image_extension($image);
        $this->handle_request(
            'image',
            $this->request->param('path'),
            $extension
        );
    }
    protected function handle_request($action, $path, $extension)
    {
        $config_key = Inflector::plural($action);
        $file = $this->config[$config_key]['directory'].$path;
        if ($this->find_file($file, $extension))
        {
            $this->serve_file($file, $extension);
        }
        else
        {
            $this->error();
        }
    }
    protected function find_file($file, $extension)
    {
        $path_parts = pathinfo($file);
        return Kohana::find_file('media', $path_parts['dirname']."/".$path_parts['filename'], $extension);
    }
    protected function find_image_extension($file)
    {
        foreach ($this->config['images']['extension'] as $extension)
        {
            if ($this->find_file($file, $extension) !== FALSE)
            {
                return $extension;
            }
        }
        return FALSE;
    }
    protected function serve_file($file, $extension)
    {
        $path = $this->find_file($file, $extension);
        $this->response->headers('Content-Type', File::mime_by_ext($extension));
        $this->response->headers('Content-Length', (string) filesize($path));
        $this->response->headers('Cache-Control','max-age=86400, public');
        $this->response->headers('Expires', gmdate('D, d M Y H:i:s 'G'M'T', time() + 86400));
        $this->response->body(file_get_contents($path));
    }
    protected function error()
    {
        throw new HTTP_Exception_404('File :file not found.', array(
            ':file' => $this->request->param('path', NULL),
        ));
    }
}

/config/media.php

return array(
    'styles' => array(
        'directory'       => 'css/',
        'extension'       => 'css',
    ),
    'scripts' => array(
        'directory'       => 'js/',
        'extension'       => 'js',
    ),
    'images' => array(
        'directory'       => 'img/',
        'extension'       => array('png', 'jpg', 'jpeg', 'gif', 'ico', 'svg'),
    ),
);

routes.php

Route::set('media', 'media/<action>(/<path>)', array(
        'path' => '.*?',
    ))
    ->defaults(array(
        'controller' => 'media',
        'action' => 'index',
    ));

默认.htaccess应该处理根目录中的任何内容,而不需要任何额外的代码。

/webroot 
--> application/
--> modules/
--> system
--> css/
--> scripts/

来自的任何呼叫http://domain.com/css将正确地在css/中查找,因为.htaccess规则首先查找现有文件/文件夹,然后从Kohana加载index.php。