PHP开发框架kohana3 自定义路由设置示例,框架kohana3
PHP开发框架kohana3 自定义路由设置示例,框架kohana3
由于kohana框架在国内的用户较少,而且新版与kohana2.X差别太大,加之kohana3资料多为英文.让很多对kohana感兴趣,想学的同学们只能望K兴叹.由于公司 最近转向kohana3开发(kohana3.1.0稳定版),所以我趁次机会,认真看了下官方的资料.获益良多,借个人小站,分享给大家.今天呢,就说下kohana的路由设置.
再次说下,我用的是ko3.1.0与ko3有差别的.
其实呢,kohana3的路由设置,很简单.打开application文件下的bootstrap.php,找到Route::set,会看如下的默认路由:
复制代码 代码如下:
Route::set('default', '((/(/)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
这个呢,就是默认的路由了,可以看出它的组成是这样子的.名称,控制器,动作,参数.特别要指出的是,每个路由必须指定默认的控制和动作,一般是index.
如何创建自定义的路由呢,其实和默认的写法是一样只不过,加入自己要加的东西.例如有一个产品列表页,需要获取产品类型的ID和当前页码.
路由可以这样设置
复制代码 代码如下:
Route::set('product', 'product/((/)(/))')
->defaults(array(
'controller' => 'product',
'action' => 'index',
'id' =>0,
'page' =>0//有的例子这里是NULL,但是我用了报错.
));
这里呢,第一个product是名称,后面的是重点讲的地方,product是控制器,/action是动作,一定要这么写.后面(/)里面是参数.在页面中获取这里参数是这样,$id = $this->request->param('id'),这个里面的id,与路由里面的id名称必须相同.
需要的同学可参照这个例子去改,应该可以了.未了,强烈推荐大家看2个站的东西
1.http://kohanaframework.org/3.1/guide(官方的在线文档)
2.http://kerkness.ca/wiki/doku.php(非官方的维基,例子比官方的好,但是版本3.0的)
可以对比着看,相信大家一定可以玩转kohana,加油!!
not found MODPATH\\database\\classes\\kohana\\db.php [ 63 ] 58 * @param请写明你在流程中是如何调用database或ORM的,也可能是你调用的方法不对。
平常我也不发言。今天写个代码给你看看:简单的Model层
product.class.php:
class product{
public function getAllProducts(){
$q="SELECT * FROM Product";
$r=$db->query($q);
$proArr=array();
while($row=db->fetchAssoc($r)){
$proArr[]=$row;
}
return $proArr;
}
}
?>
视图和控制层:
getallproducts.php:
$product=new product();
$ps=$product->getAllProducts();
foreach($ps as $p){
//输出数据库中查出来的
echo $p['name'];
}
我一般就是这样写的 PHP说多了,就是大部分对数组进行编程 ,
大部分都是在页面里输出SQL语句,嵌套HTML,显得页面臃肿,不易维护和扩展
这样分层以后修改比较简单

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
