网站建设和微信小程序,公司网站的建设内容怎么写,江苏公众科技网站建设,政务网站建设目标Kohana框架的安装及部署 tipsKohana安装以及部署1、重要文件作用说明1.1 /index.php1.2 /application/bootstrap.php 2、项目结构3、路由配置3.1、隐藏项目入口的路由3.2、配置默认路由3.3、配置自定义的路由(Controller目录下的控制器)3.4、配置自定义的路由(Controller/direc… Kohana框架的安装及部署 tipsKohana安装以及部署1、重要文件作用说明1.1 /index.php1.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文件中你可以进行以下操作
定义常量你可以定义一些全局常量如应用程序的根目录路径、环境变量等。加载必要的文件bootstrap.php会加载一些必要的文件包括Autoloader类、Exception处理类等。配置框架你可以设置框架的各种配置选项如默认时区、错误报告级别、数据库连接等。注册模块如果你使用了额外的模块你可以在这里注册它们以便框架能够正确加载和使用它们。运行应用程序最后bootstrap.php会运行应用程序启动请求处理流程并将控制权交给应用程序的入口文件index.php。配置路由规则映射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 symlinkd index.php
if ( ! is_dir($application) AND is_dir(DOCROOT.$application))$application DOCROOT.$application;// Make the modules relative to the docroot, for symlinkd index.php
if ( ! is_dir($modules) AND is_dir(DOCROOT.$modules))$modules DOCROOT.$modules;// Make the system relative to the docroot, for symlinkd 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 checkreturn 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 corerequire APPPATH.classes/Kohana.EXT;
}
else
{// Load empty core extensionrequire 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 Couldnt 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 authenticationcache MODPATH.cache, // Caching with multiple backendscodebench MODPATH.codebench, // Benchmarking tooldatabase MODPATH.database, // Database accessimage MODPATH.image, // Image manipulationminion MODPATH.minion, // CLI Tasksorm MODPATH.orm, // Object Relationship Mappingunittest MODPATH.unittest, // Unit testinguserguide 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-demoD:\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直接映射项目路径myappD:\Env\wamp64\www\my_app http://myapp/index.php/myapi/get_datahttp://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 通过虚拟映射域名访问myappD:\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**