有人可以用如何的Kohana 3.2.2例3.2.2
Q
例3.2.2
0
A
回答
1
這裏是我做過什麼: 在/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();
}
}
然後在你看來,你只需要使用這個 /應用/視圖/網站。 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');?>
0
使用外部文件(CSS,JavaScript和圖像)的例子幫助取決於通過你的控制器運行它們或不。一個示例控制器,如果您想使用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',
));
0
你的默認的.htaccess應該處理的事情你的根目錄沒有任何額外的代碼。
/webroot
--> application/
--> modules/
--> system
--> css/
--> scripts/
任何從http://domain.com/css稱爲將適當看在CSS /因爲規則的.htaccess查找現有的文件/文件夾,然後再加載從Kohana中的index.php文件中。
相關問題
- 1. Robolectric 3.2.2獲取影子類的實例?
- 2. favicon.ico和CherryPy 3.2.2
- 3. ActiveMQ-CPP prefetchSize 3.2.2
- 4. Maven 3.2.2 packagingExcludes
- 5. 管理導軌3.2.2
- 6. R 3.2.2 - Windows 8.1 - 無法運行R-3.2.2-win.exe(安裝程序)
- 7. R 3.2.2中的chooseCRANmirror()出錯
- 8. JBoss 3.2.2和JDBC升級
- 9. 問題與cx_freeze和python 3.2.2?
- 10. Azure服務總線3.2.2 BeginReceive()
- 11. 從Xcode 3.2.2到Xcode 3.2.3
- 12. 從Xcode 3.2.2移到Xcode 3.2.3?
- 13. 使用Rails安裝Mongoid 3.2.2
- 14. Spring 3.2.2和Hibernate版本
- 15. 在R 3.2.2中加載.RData
- 16. pytest -3.2.2重複測試
- 17. 無法解決:org.roboelectric:roboelectric:3.2.2
- 18. Spring 3.2.2 + Tiles3 + i18n找不到
- 19. 篩選結果(3.2.2的Joomla)
- 20. Xcode 3.2.2和本地化Settings.bundle
- 21. facebook登出php v.3.2.2
- 22. 的Rails 3.2.2不執行RJS
- 23. 從shell使用Processing-3.2.2
- 24. 使用CMake構建gdcm-2.4.3 3.2.2
- 25. 如何升級設計2.1.2設計3.2.2
- 26. 每天午夜執行任務 - Grails 3.2.2
- 27. 的Python 3.2.2錯誤局部變量
- 28. Rails 3.2.2操作被調用兩次
- 29. YAJL安裝和構建與xcode 3.2.2
- 30. Spring 3.2.2屬性佔位符不工作
感謝邁克......我是一個相當新手的框架..是route.php相同的bootstrap.php或究竟是什麼?以及如何從視圖和鏈接到視圖的控制器中調用此方法。提前致謝。 – wanjiku
對不起,這條路線實際上是我們將它分開的引導線,因爲它太大了。例如,當您的img,CSS或js src爲/ media/img/file name.jpg時,控制器啓動 – Mike