alog / nin
This package is auto-updated.
Last update: 2024-05-10 17:03:14 UTC
README
Nin是一个基于Yii的一些想法的极简PHP框架。它代表"No It's Not",是Yii的"Yes It Is"的谐音。
它是如何工作的?
Nin是一个MVC系统,其中Nin\Model
和Nin\Controller
是关键类,视图是包含PHP文件的。
入门指南
通过Composer安装依赖项,可以快速开始使用Nin。您可以在Packagist上找到该软件包(点击访问)。通过运行以下命令安装它:
$ composer require codecat/nin
然后创建index.php
include 'vendor/autoload.php'; nf_begin();
详细安装
还有一个方便的Docker镜像可以用来配置预配置的web服务器和PHP。滚动到readme文件的底部了解更多信息。
您还可以从Github(或master分支)下载版本,并从其他地方包含Nin。根据您的web服务器,您可能还需要配置一个重写规则,以便将一切指向index.php
。可以在server-configs
中找到流行web服务器的示例配置和基本说明。我建议使用Caddy与php-fpm,因为它工作得非常好,默认情况下已经正确路由到index.php
。
最简示例
在调用nf_begin
后,您将自动拥有1个路由:/
。默认情况下,它指向IndexController.Index
。这意味着它将寻找一个名为IndexController
的类以及该类中的一个名为actionIndex
的方法。
要创建控制器类,在index.php
所在的目录中创建一个名为controllers
的文件夹。在这个文件夹内,我们可以创建一个名为IndexController.php
的文件。
class IndexController extends \Nin\Controller { public function actionIndex() { echo 'This is the index page!'; } }
路由是通过controller.action
来工作的,其中controller
是一个类名,而action
是类中前缀为action
的对应动作名。例如,FooController.Bar
将实例化FooController
并调用它的actionBar
方法。
路由是通过使用nf_route
函数来定义的
nf_route('/', 'IndexController.Home'); nf_route('/info', 'IndexController.Info'); nf_route('/user/:username', 'UserController.Profile');
上述示例中的最后一个nf_route
调用在其路径中有一个特殊的参数,:username
。这将成为动作方法中的一个参数。所以在上面的例子中,第三个路由将使用这个控制器类
class UserController extends \Nin\Controller { public function actionProfile(string $username) { // Do something with $username } }
注意,您还可以指定方法参数的类型,以自动将其转换为正确的类型。例如,int $id
将确保您确实得到了一个整数参数。
动作方法参数也可以使用从$_GET
获取的URL参数来设置。例如,如果您的路由定义简单为/user
,则您仍然可以通过将URL设置为?username=foo
来设置string $username
。当URL中没有提供username
时,Nin将抛出一个关于缺失必需参数的错误。您可以通过给方法参数一个默认值来使参数可选,例如:string $username = 'admin'
。
如果控制器构造函数接受参数,参数也将传递给控制器构造函数。它们的行为与方法参数完全相同。例如,如果您有一个指向 UserController::Posts
的路由 /user/:id/posts
,您可以使用以下控制器
class UserController extends \Nin\Controller { private $user; public function __construct(int $id) { $this->user = User::findByPk($id); } public function actionPosts() { // Do something with $this->user } }
使用视图
您还可以在控制器动作方法中做以下操作
$this->render('foo');
这将渲染位于 views/controller/view.php
的 foo
视图。所以如果上面的行在 IndexController
中,视图将位于 views/index/foo.php
。
您还可以像这样向 render()
函数传递参数
$this->render('foo', [ 'bar' => 'hello ', 'foobar' => 'world' ]);
您的视图可以使用这些参数,就像数组中的键是 PHP 变量一样
<p>The controller says: <b><?= $bar . $foobar ?></b></p>
如果您在 views/layout.php
中创建了一个布局文件,您可以用它作为视图的包装器。它将为渲染的视图公开 $content
变量。例如,它可能看起来像这样
<!doctype html> <html> <head> <title>My website</title> </head> <body> <h1>My website</h1> <div class="content"> <?= $content ?> </div> </body> </html>
使用数据库
Nin 支持 PostgreSQL、MySQL 和 SQLite。要开始使用 Nin 的 PostgreSQL 等数据库,请将数据库连接信息指定为 nf_begin
的参数
nf_begin([ 'postgres' => [ 'hostname' => 'localhost', 'password' => 'password', ], ]);
一旦设置此配置,您就可以创建并使用模型了。
注意,上面的 postgres
键是更详细数据库配置的快捷键
nf_begin([ 'db' => [ 'class' => 'Postgres', 'options' => [ 'hostname' => 'localhost', 'password' => 'password', ], ], ]);
更详细的配置允许您在需要时实现其他第三方数据库上下文和查询构建器。
使用模型
模型定义为类。静态函数用于配置模型的行为。例如,用户模型如下所示
class User extends \Nin\Model { public static function tablename() { return 'users'; } }
默认情况下,模型的主键定义为 ID
,但可以通过定义一个静态函数 primarykey
来更改
public static function primarykey() { return 'id'; }
这告诉 Nin 包含该模型行数据的数据库表名为 users
。
您现在可以使用新的 User
类执行所有类型的操作。要创建一个新用户
$user = new User(); $user->Username = 'admin'; $user->Password = 'hunter2'; $user->Age = 24; $user->save(); // Nin will automatically set the ID of the model after inserting it echo 'New user ID: ' . $user->ID;
(旁注:请确保在数据库中对密码进行强散列,上面的示例仅用于演示目的!)
根据 ID 查找用户
$user = User::findByPk(1); if (!$user) { die('No user found!'); } echo 'Hello, ' . $user->Username;
根据属性(列值)查找用户
$user = User::findByAttributes([ 'Username' => 'admin' ]); if (!$user) { die('No user found!'); } echo 'Hello, ' . $user->Username;
您还可以使用 findAll
和 findAllByAttributes
来获取多个模型。它们返回数组,并按预期工作
$users = User::findAll(); $users = User::findAllByAttributes([ 'Age' => 24 ]);
您可以选择性地将选项作为数组传递给 findAll
和 findAllByAttributes
的第二个参数。接受的键是
group
使用其值执行分组查询order
对象排序的顺序(默认为ASC
)orderby
排序对象时使用的列(默认为主键)limit
您想要获取的最多个数,或用于项目范围的数组
模型还可以有 关系 属性。例如,如果用户可以有多个帖子,您将定义 User
类如下所示
class User extends \Nin\Model { public static function tablename() { return 'users'; } public function relations() { return [ 'posts' => [ HAS_MANY, 'Post', 'Author' ], ]; } }
然后您可以使用 $user->posts
简单地获取模型 Post
的对象数组,使用帖子列 Author
。您可以定义以下 3 种关系类型
BELONGS_TO
使用 他们的类名 和 我的列 查找一个对象HAS_MANY
使用 他们的类名 和 他们的列 查找多个对象HAS_ONE
使用 他们的类名 和 他们的列 查找一个对象
中间件
您可以针对特定路由设置中间件。为此,请使用函数 nf_route_middleware_begin
和 nf_route_middleware_end
将您的 nf_route
调用包装起来。例如,要要求用户对某些路由进行登录,您可以这样做
nf_route_middleware_begin(new Nin\Middleware\AuthSession()); nf_route('/posts', 'PostsController.Index'); nf_route('/users', 'UsersController.Index'); nf_route_middleware_end();
当某人导航到 /posts
或 /users
并未登录(通过 Nin\Nin::setuid()
提供的用户会话管理)时,请求将停止并且永远不会到达控制器。此外,默认情况下,AuthSession
将将用户重定向到 /login
,因此用户可以登录。
中间件可以通过多次调用 nf_route_middleware_begin
来堆叠。请注意,您还必须以相同的次数调用 nf_route_middleware_end
,否则Nin会抛出错误。
Docker
Nin 还可以作为基于 caddy:alpine
的 Docker 镜像 使用。以下是一个如何在 Dockerfile 中使用 Nin 的快速示例
FROM codecatt/nin:2.0
COPY . /var/www/html
然后,在您的 index.php
文件中,您可以像这样包含 Nin
include('../nin/nf.php');
请注意,您不需要进行任何手动配置,因为这由 Docker 镜像自动处理。
有多个标签可供选择
latest
:最新版本。2.0
:最新 2.0.x 版本。1.6
:最新 1.6.x 版本。1.3
:最新 1.3.x 版本。1.2
:最新 1.2.x 版本。