Kohana框架的安装及部署

Kohana框架的安装及部署

  • tips
  • Kohana安装以及部署
    • 1、重要文件作用说明
      • 1.1 /index.php
      • 1.2 /application/bootstrap.php
    • 2、项目结构
    • 3、路由配置
      • 3.1、隐藏项目入口的路由
      • 3.2、配置默认路由
      • 3.3、配置自定义的路由(Controller目录下的控制器)
      • 3.4、配置自定义的路由(Controller/directory下的控制器)

tips

kohana官网:https://kohanaframework.org/

kohana中文文档:https://github.com/stenote/Kohana_Docs_zh_CN/tree/master

Kohana安装以及部署

在官网下载kohana压缩包,解压到自己web项目目录下

kohana框架是一个轻量级的PHP开发框架,包结构主要分为4个部分:

  • application目录:应用程序的主要目录,包含了控制器、模型、视图和其他自定义代码文件。
  • system目录:这是Kohana框架的核心目录,包含了框架的各种类和库文件。通常不需要修改这些文件,除非你有特定的需求。
  • modules目录:这个目录用于存放可重用的模块。每个模块都可以有自己的MVC结构,类似于应用程序的结构。
  • public目录:这是Web服务器的根目录,其中包含了入口文件index.php和一些静态资源文件,如CSS、JavaScript和图片等。

1、重要文件作用说明

  • /index.php

index.php:这是Kohana应用程序的入口文件,位于public目录下。当Web服务器接收到请求时,会将请求交给index.php处理。该文件负责初始化框架并调度请求到相应的控制器和动作。它通常包含一些必要的设置和引入bootstrap.php文件的代码。

index.php是Kohana应用程序的入口文件,用于处理请求和调度控制器

  • /install.php

install.php:这是一个安装脚本,用于在初始部署时设置Kohana框架。它位于系统目录下,主要用于执行一些初始化操作,如创建数据库表、设置文件权限等。一旦完成安装过程,通常建议删除或禁用install.php文件,以防止未经授权的访问。

install.php是一个安装脚本,用于执行初始设置和配置。

  • /application/bootstrap.php

在Kohana框架中,bootstrap.php是一个重要的文件,它位于系统目录下。这个文件主要用于初始化框架和应用程序的各种设置。

在bootstrap.php文件中,你可以进行以下操作:

  1. 定义常量:你可以定义一些全局常量,如应用程序的根目录路径、环境变量等。
  2. 加载必要的文件:bootstrap.php会加载一些必要的文件,包括Autoloader类、Exception处理类等。
  3. 配置框架:你可以设置框架的各种配置选项,如默认时区、错误报告级别、数据库连接等。
  4. 注册模块:如果你使用了额外的模块,你可以在这里注册它们,以便框架能够正确加载和使用它们。
  5. 运行应用程序:最后,bootstrap.php会运行应用程序,启动请求处理流程,并将控制权交给应用程序的入口文件index.php。
  6. 配置路由规则,映射controller下的接口

bootstrap.php文件负责初始化框架和应用程序的各种设置,为应用程序的正常运行做好准备工作。

1.1 /index.php

<?php

// application模块的包名
$application = 'application';

// modules模块的包名
$modules = 'modules';

// system模块的包名
$system = 'system';

// 定义EXT为.php,即PHP文件后缀
define('EXT', '.php');

// 设置错误报告级别为全部:严重错误、警告、通知,严格编码规范及最佳实践
error_reporting(E_ALL | E_STRICT);




// 定义常量DOCROOT 为 /,即全路径为根路径/
// Set the full path to the docroot
define('DOCROOT', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);

// 校验包路径是否为文件夹
// Make the application relative to the docroot, for symlink'd index.php
if ( ! is_dir($application) AND is_dir(DOCROOT.$application))
	$application = DOCROOT.$application;

// Make the modules relative to the docroot, for symlink'd index.php
if ( ! is_dir($modules) AND is_dir(DOCROOT.$modules))
	$modules = DOCROOT.$modules;

// Make the system relative to the docroot, for symlink'd index.php
if ( ! is_dir($system) AND is_dir(DOCROOT.$system))
	$system = DOCROOT.$system;

// Define the absolute paths for configured directories
// 为配置好的包定义全路径常量名,即APPPATh为application包的全路径,MODPATH为modules包的全路径,SYSPATH为system包的全路径
define('APPPATH', realpath($application).DIRECTORY_SEPARATOR);
define('MODPATH', realpath($modules).DIRECTORY_SEPARATOR);
define('SYSPATH', realpath($system).DIRECTORY_SEPARATOR);

// Clean up the configuration vars
// 清除变量
unset($application, $modules, $system);

// 判断install.php文件是否存在,如果存在,则引入
if (file_exists('install'.EXT))
{
	// Load the installation check
	return include 'install'.EXT;
}
// 设置应用的启动时间常量
/**
 * Define the start time of the application, used for profiling.
 */
if ( ! defined('KOHANA_START_TIME'))
{
	define('KOHANA_START_TIME', microtime(TRUE));
}

/**
 * Define the memory usage at the start of the application, used for profiling.
 */
// 设置当前系统内存使用情况常量
if ( ! defined('KOHANA_START_MEMORY'))
{
	define('KOHANA_START_MEMORY', memory_get_usage());
}

// 引入bootstrap文件(重要)
// Bootstrap the application
require APPPATH.'bootstrap'.EXT;

// 判断当前运行环境是否是命令行环境,根据环境运行应用程序
if (PHP_SAPI == 'cli') // Try and load minion
{
	class_exists('Minion_Task') OR die('Please enable the Minion module for CLI support.');
	set_exception_handler(array('Minion_Exception', 'handler'));

    // 执行命令行任务
	Minion_Task::factory(Minion_CLI::options())->execute();
}
else
{
	/**
	 * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
	 * If no source is specified, the URI will be automatically detected.
	 */
	echo Request::factory(TRUE, array(), FALSE)// 创建请求对象,用于处理HTTP请求
        // 把请求处理结果输出到浏览器
		->execute()
		->send_headers(TRUE)
		->body();
}

1.2 /application/bootstrap.php

<?php defined('SYSPATH') or die('No direct script access.');

// -- Environment setup --------------------------------------------------------

// Load the core Kohana class
// 加载kohana的核心库
require SYSPATH.'classes/Kohana/Core'.EXT;

// 在application包下或者system包下引入Kohana.php文件
if (is_file(APPPATH.'classes/Kohana'.EXT))
{
	// Application extends the core
	require APPPATH.'classes/Kohana'.EXT;
}
else
{
	// Load empty core extension
	require SYSPATH.'classes/Kohana'.EXT;
}

/**
 * Set the default time zone.
 *
 * @link http://kohanaframework.org/guide/using.configuration
 * @link http://www.php.net/manual/timezones
 */
// 设置默认时区
date_default_timezone_set('America/Chicago');

/**
 * Set the default locale.
 *
 * @link http://kohanaframework.org/guide/using.configuration
 * @link http://www.php.net/manual/function.setlocale
 */
// 设置语言环境
setlocale(LC_ALL, 'en_US.utf-8');

/**
 * Enable the Kohana auto-loader.
 *
 * @link http://kohanaframework.org/guide/using.autoloading
 * @link http://www.php.net/manual/function.spl-autoload-register
 */
// 允许kohana的自动加载器
spl_autoload_register(array('Kohana', 'auto_load'));

/**
 * Optionally, you can enable a compatibility auto-loader for use with
 * older modules that have not been updated for PSR-0.
 *
 * It is recommended to not enable this unless absolutely necessary.
 */
//spl_autoload_register(array('Kohana', 'auto_load_lowercase'));

/**
 * Enable the Kohana auto-loader for unserialization.
 *
 * @link http://www.php.net/manual/function.spl-autoload-call
 * @link http://www.php.net/manual/var.configuration#unserialize-callback-func
 */
ini_set('unserialize_callback_func', 'spl_autoload_call');

/**
 * Set the mb_substitute_character to "none"
 *
 * @link http://www.php.net/manual/function.mb-substitute-character.php
 */
mb_substitute_character('none');

// -- Configuration and initialization -----------------------------------------

/**
 * Set the default language
 */
I18n::lang('en-us');

if (isset($_SERVER['SERVER_PROTOCOL']))
{
	// Replace the default protocol.
	HTTP::$protocol = $_SERVER['SERVER_PROTOCOL'];
}

/**
 * Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
 *
 * Note: If you supply an invalid environment name, a PHP warning will be thrown
 * saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
 */
//设置kohana的环境常量
if (isset($_SERVER['KOHANA_ENV']))
{
	Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
}

/**
 * Initialize Kohana, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - integer  cache_life  lifetime, in seconds, of items cached              60
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 * - boolean  expose      set the X-Powered-By header                        FALSE
 */
Kohana::init(array(
	'base_url'   => '/',
));

/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Log_File(APPPATH.'logs'));

/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File);

/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
// 引入其他模块
Kohana::modules(array(
	 'auth'       => MODPATH.'auth',       // Basic authentication
	 'cache'      => MODPATH.'cache',      // Caching with multiple backends
	 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
	 'database'   => MODPATH.'database',   // Database access
	 'image'      => MODPATH.'image',      // Image manipulation
	 'minion'     => MODPATH.'minion',     // CLI Tasks
	 'orm'        => MODPATH.'orm',        // Object Relationship Mapping
	 'unittest'   => MODPATH.'unittest',   // Unit testing
	 'userguide'  => MODPATH.'userguide',  // User guide and API documentation
	));

/**
 * Cookie Salt
 * @see  http://kohanaframework.org/3.3/guide/kohana/cookies
 * 
 * If you have not defined a cookie salt in your Cookie class then
 * uncomment the line below and define a preferrably long salt.
 */
// Cookie::$salt = NULL;
// 如果需要正常启动项目:需要给这个字段设置一个值(string),如Cookie::$salt = 'foobar';
/**
 * Set the routes. Each route must have a minimum of a name, a URI and a set of
 * defaults for the URI.
 */
// 设置接口的路由规则
Route::set('default', '(<controller>(/<action>(/<id>)))')
	->defaults(array(
		'controller' => 'welcome',
		'action'     => 'index',
	));

2、项目结构

在这里插入图片描述

3、路由配置

  • tip:在bootstrap.php文件中配置路由
  • my_app项目放在warmserver的www目录下
  • warmserver重新写虚拟映射之后,需要关闭warmserve再重新启动

3.1、隐藏项目入口的路由

​ kohana项目的入口是application目录下的index.php文件,因此如果需要访问项目的某个接口,需要先访问到项目入口index.php,这样在url上会显得不简洁,因此可以通过配置.htaccess文件,来隐藏url中默认的项目入口。把官网的kahana项目拉下来,项目根目录下会有默认的example.htaccess文件,把前缀名字去掉即可,即变成.htaccess

eg:
前提:需要先使用nginx或者apache服务器映射项目路径跟本地的项目文件
    映射:http://my_app  =>  D:\PhpCode\kohana-demo
	D:\PhpCode\kohana-demo路径下即是官网拉下来的kohana项目,重新命名成了kohana-demo
配置前:
   	如果需要访问  http://localhost/my_app项目
	则需要输入url:http://localhost/my_app/index.php
注意:通过www目录映射的localhost访问www目录下的my_app项目时,url中的index.php不能省略,如http://localhost/my_app/index.php/   中的index.php/不能省略

3.2、配置默认路由

  • 需要注意拉下来的kohana项目默认配置了默认路由
  • 默认的路由配置需要在bootstrap.php文件的最下面,否则默认路由会匹配全部请求,就不会匹配到后面的路由了
  • 默认的路由映射,只要访问到该项目,即默认会路由到该接口,即访问到**/application/classes/Controller/welcome/index**
  • Controller/Welcome.php文件下的action_index方法
  • 在bootstrap.php中配置
Route::set('default', '(<controller>(/<action>(/<id>)))')
	->defaults(array(
		'controller' => 'welcome',
		'action'     => 'index',
	));
  • Controller/Welcome.php
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Welcome extends Controller {

	public function action_index()
	{
		$this->response->body('hello world');
	}

} // End Welcome
  • 访问默认的接口/Controller/Welcome/action_index
    • 通过localhost映射的www目录访问:
      • http://localhost/my_app/index.php/ 注意,末尾的/不能省略
    • 通过虚拟域名访问
      • http://myapp/
  • 访问默认的**/Controller/welcome/index**

在这里插入图片描述

3.3、配置自定义的路由(Controller目录下的控制器)

  • /Controller下创建myapi.php文件
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Myapi extends Controller {

    public function action_get_data()
    {
        $this->response->body('myapi');
    }
}
  • bootstrap.php中配置
// 配置Controller下的某个控制器的路由
Route::set('myapi', 'myapi/<action>')
    ->defaults(array(
        'controller' => 'myapi',
        'action'     => 'index',
    ));
  • 访问接口
    • 通过www文件夹映射的localhost访问:localhost => D:\Env\wamp64\www
      • http://localhost/my_app/index.php/myapi/get_data
    • 通过myapp直接映射项目路径:myapp=>D:\Env\wamp64\www\my_app
      • http://myapp/index.php/myapi/get_data
      • http://myapp/myapi/get_data(省略index.php)
  • 访问**/Controller/myapi/get_data**

在这里插入图片描述

3.4、配置自定义的路由(Controller/directory下的控制器)

  • bootstrap.php中配置
// 配置Controller目录下的某个文件夹下的控制器的路由
Route::set('api', 'api(/<controller>(/<action>))')
    ->defaults(array(
        'directory' => 'api',
        'controller' => 'default',
        'action' => 'index',
    ));
  • 创建Controller/api/game.php文件
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Api_Game extends Controller {

    public function action_get_data()
    {
        $this->response->body('game');
    }
}
  • 访问接口
    • 通过www文件夹映射的localhost访问:localhost => D:\Env\wamp64\www
      • http://localhost/my_app/index.php/api/game/get_data
    • 通过虚拟映射域名访问:myapp=>D:\Env\wamp64\www\my_app
      • http://myapp/index.php/api/game/get_data
      • 或http://myapp/api/game/get_data(省略index.php)
  • 访问**/Controller/api/game/get_data**

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/146749.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

JS操作canvas

<canvas>元素本身并不可见&#xff0c;它只是创建了一个绘图表面并向客户端js暴露了强大的绘图API。 1 <canvas> 与图形 为优化图片质量&#xff0c;不要在HTML中使用width和height属性设置画布的屏幕大小。而要使用CSS的样式属性width和height来设置画布在屏幕…

父组件用ref获取子组件数据

子组件 Son/index.vue 子组件的数据和方法一定要记得用defineExpose暴露&#xff0c;不然父组件用ref是获取不到的&#xff01;&#xff01;&#xff01; <script setup> import { ref } from "vue"; const sonNum ref(1); const changeSon () > {sonNum.…

DAY54 392.判断子序列 + 115.不同的子序列

392.判断子序列 题目要求&#xff1a;给定字符串 s 和 t &#xff0c;判断 s 是否为 t 的子序列。 字符串的一个子序列是原始字符串删除一些&#xff08;也可以不删除&#xff09;字符而不改变剩余字符相对位置形成的新字符串。&#xff08;例如&#xff0c;"ace"是…

探秘Vue组件间通信:详解各种方式助你实现目标轻松搞定!

&#x1f3ac; 江城开朗的豌豆&#xff1a;个人主页 &#x1f525; 个人专栏 :《 VUE 》 《 javaScript 》 &#x1f4dd; 个人网站 :《 江城开朗的豌豆&#x1fadb; 》 ⛺️ 生活的理想&#xff0c;就是为了理想的生活 ! ​ 目录 ⭐ 专栏简介 &#x1f4d8; 文章引言 一…

threejs(13)-着色器设置点材质

着色器材质内置变量 three.js着色器的内置变量&#xff0c;分别是 gl_PointSize&#xff1a;在点渲染模式中&#xff0c;控制方形点区域渲染像素大小&#xff08;注意这里是像素大小&#xff0c;而不是three.js单位&#xff0c;因此在移动相机是&#xff0c;所看到该点在屏幕…

基于单片机的电源切换控制器设计(论文+源码)

1.系统设计 在基于单片机的电源切换控制器设计中&#xff0c;系统功能设计如下&#xff1a; &#xff08;1&#xff09;实现电源的电压检测&#xff1b; &#xff08;2&#xff09;如果电压太高&#xff0c;通过蜂鸣器进行报警提示&#xff0c;继电器进行切换&#xff0c;使…

Idea 编译SpringBoot项目Kotlin报错/Idea重新编译

原因应该是一次性修改了大量的文件, SpringBoot项目启动Kotlin报错, Build Project也是同样的结果, 报错如下 Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.1.13. Build-&…

python语言的由来与发展历程

Python语言的由来可以追溯到1989年&#xff0c;由Guido van Rossum&#xff08;吉多范罗苏姆&#xff09;创造。在他的业余时间里&#xff0c;Guido van Rossum为了打发时间&#xff0c;决定创造一种新的编程语言。他受到了ABC语言的启发&#xff0c;ABC语言是一种过程式编程语…

Hadoop-HDFS架构与设计

HDFS架构与设计 一、背景和起源二、HDFS概述1.设计原则1.1 硬件错误1.2 流水访问1.3 海量数据1.4 简单一致性模型1.5 移动计算而不是移动数据1.6 平台兼容性 2.HDFS适用场景3.HDFS不适用场景 三、HDFS架构图1.架构图2.Namenode3.Datanode 四、HDFS数据存储1.数据块存储2.副本机…

亚马逊云AI大语言模型应用下的创新Amazon Transcribe的使用

Transcribe简介 语音识别技术&#xff0c;也被称为自动语音识别&#xff08;Automatic Speech Recognition&#xff0c;简称ASR&#xff09;&#xff0c;其目标是将人类的语音中的词汇内容转换为计算机可读的输入&#xff0c;例如按键、二进制编码或者字符序列。语音识别技术已…

2023-11-14 LeetCode每日一题(阈值距离内邻居最少的城市)

2023-11-14每日一题 一、题目编号 1334. 阈值距离内邻居最少的城市二、题目链接 点击跳转到题目位置 三、题目描述 有 n 个城市&#xff0c;按从 0 到 n-1 编号。给你一个边数组 edges&#xff0c;其中 edges[i] [fromi, toi, weighti] 代表 fromi 和 toi 两个城市之间的…

阿里达摩院开源DAMO-YOLO

1.简介 DAMO-YOLO是一个兼顾速度与精度的目标检测框架&#xff0c;其效果超越了目前的一众YOLO系列方法&#xff0c;在实现SOTA的同时&#xff0c;保持了很高的推理速度。DAMO-YOLO是在YOLO框架基础上引入了一系列新技术&#xff0c;对整个检测框架进行了大幅的修改。具体包括…

c语言从入门到实战——基于指针的数组与指针数组

基于指针的数组与指针数组 前言1. 数组名的理解2. 使用指针访问数组3. 一维数组传参的本质4. 冒泡排序5. 二级指针6. 指针数组7. 指针数组模拟二维数组 前言 指针的数组是指数组中的元素都是指针类型&#xff0c;它们指向某种数据类型的变量。 1. 数组名的理解 我们在使用指针…

Excel-快速将公式运用到一整列

先在该列的第一个单元格里写好公式&#xff0c;然后单击该单元格 在图中标示的地方输入我们需要填充的单元格区域 同时按住Ctrl和Enter键&#xff0c;这时需要填充的单元格区域就都被选中了 然后单击一下图中公式的后面&#xff0c;再次按下Ctrl和Enter键&#xff0c;这样就完…

第3章:搜索与图论【AcWing】

文章目录 图的概念图的概念图的分类有向图和无向图 连通性连通块重边和自环稠密图和稀疏图参考资料 图的存储方式邻接表代码 邻接矩阵 DFS全排列问题题目描述思路回溯标记剪枝代码时间复杂度 [N 皇后问题](https://www.luogu.com.cn/problem/P1219)题目描述全排列思路 O ( n ! …

Unity--互动组件(Toggle)

1.组件的可交互 2.组件的过渡状态 3.组件的导航 4.Toggle的属性和参数设置 Toggle 切换控制是一个复选框&#xff0c;允许用户打开或关闭的一个选项&#xff1b; ”Toggle的属性和参数&#xff1a;“” Is on&#xff1a;&#xff08;开启&#xff09; 拨动开关是否从一开…

二叉树基础

前言 我们好久没有更新数据结构的博文了&#xff0c;今天来更新一期树&#xff01;前几期我们已经介绍了顺序表、链表&#xff0c;栈和队列等基本的线性数据结构并对其分别做了实现&#xff0c;本期我们再来介绍一个灰常重要的非线性基本结构 ---- 树型结构。 本期内容介绍 树…

计算机 - - - 浏览器网页打开本地exe程序,网页打开微信,网页打开迅雷

效果 在电脑中安装了微信和迅雷&#xff0c;可以通过在地址栏中输入weixin:打开微信&#xff0c;输入magnet:打开迅雷。 同理&#xff1a;在网页中使用a标签&#xff0c;点击后跳转链接打开weixin:&#xff0c;也会同样打开微信。 运用同样的原理&#xff0c;在网页中点击超…

第3关:集合操作100

任务描述相关知识编程要求测试说明 任务描述 本关任务&#xff1a;使用 集合操作解决实际问题 相关知识 1.集合并操作符 可转换为SQL 若R,S的属性名不同&#xff0c;可使用重命名使相应列名一致后进行并操作 例如&#xff1a;R(A,B,C) S(D,E,F) select A,B from R union sel…

【STM32】串口和printf

1.数据通信的基本知识 1.串行/并行通信 2.单工/半双工/全双工通信 类似于【广播 对讲 电话】 不是有两根线就是全双工&#xff0c;而是输入和输出都有对应的数据线。 3.同步/异步通信 区分同步/异步通信的根本&#xff1a;判断是否有时钟信号&#xff08;时钟线&#xff09;。…