opensmarty/opensmarty-starter

Opensmarty Starter 是一个基于现代设计模式基础的 PHP 框架,用于快速构建 Web 应用和 Restful API。

安装次数: 6

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放问题: 0

语言:JavaScript

类型:项目

v1.0.0 2017-12-06 15:50 UTC

This package is auto-updated.

Last update: 2024-09-27 23:54:19 UTC


README

Opensmarty Inc 测试并投入生产使用。

Latest Version on Packagist Software License

Opensmarty Starter 是一个用于快速构建 Web 应用和 Restful API 的 PHP 框架,基于现代设计模式。

它建立在流行的 Laravel 5.5 框架Vue.js 2.4Restful APIRepository 设计OAuth2JWT单元测试、分离的前端和后端层之上。

注意:您可能会注意到包含 .idea 文件夹。请不要担心,.idea 文件夹中的任何冲突文件都已按照 .gitignore 文件中的规则忽略。

Opensmarty Starter 的诞生基于以下 3 个原因

  1. 无需从头开始使用 Laravel,即可快速创建 Web 应用程序。

  2. 使用 Restful API 实现前端和后端的分离。

  3. 引入现代设计模式,为 PHP 项目提供一个更好的基础。

实战展示

Opensmarty Starter 示例

前端使用 Vue.js 和 Restful API 展示数据

邮箱: opensmarty@163.com

密码: Abc12345

观看视频

观看下面的视频教程。

YouTube: https://youtu.be/6_lxJNX0Qe0

框架概述

Opensmarty Starter 适用于以 Restful API 为导向的项目。

Restful API 作为后端层,为前端(Web 和移动应用)提供简单统一的接口。

它利用 Laravel Passport 进行受保护资源的认证。

它还提供 API 测试和框架测试的单元测试。

它包含 Angulr 主题,并提供易于参考的各个部分。

它完全利用 Repository 设计模式。

开箱即用组件

如果您不熟悉这些包中的任何一个,建议您熟悉它们,因为当您需要时它们非常有用。

框架

基础

核心

身份验证

主题

  • 支持 Bootstrap 和 jQuery 的 Angulr 主题。示例:[Angular 版本](http://flatfull.com/themes/angulr/angular/#/app/dashboard-v1) 和 [HTML 版本](http://flatfull.com/themes/angulr/html/)。

    如果您在自己的项目中使用此主题,请购买许可证

前端

  • Vue.js 用于现代网络界面的响应式组件

开始使用

请确保您已经安装了 PHP 7.1 和 composer

您可以通过选项 A 或 B 开始

A. 通过 composer 开始

在工作目录下,运行以下命令

composer create-project --prefer-dist opensmarty/opensmarty-starter opensmarty-starter

A.1. 添加到 Git 仓库 (如有必要)

https://github.com/username/repository-name.git 替换为您自己的 Git 仓库地址。

cd opensmarty-starter

git init
git add .
git commit -m 'Initial commit'

git remote add origin https://github.com/username/repository-name.git
git push -u origin master

B. 通过克隆仓库开始

将此项目克隆到您的工作目录,然后打开该目录

git clone https://github.com/opensmarty/opensmarty-starter

B.1. 添加到 Git 仓库 (如有必要)

https://github.com/username/repository-name.git 替换为您自己的 Git 仓库地址。

cd opensmarty-starter
rm -rf .git

git init
git add .
git commit -m 'Initial commit'

git remote add origin https://github.com/username/repository-name.git
git push -u origin master

安装

开发要求

安装基本组件

打开 opensmarty-starter 文件夹 (可选,仅在您不在项目根目录下时运行)

cd opensmarty-starter

所有这些命令都应该在 opensmarty-starter 项目的根目录下执行

安装 composer 依赖

composer install

使用 Yarn 依赖管理器安装 npm 依赖(建议使用)

注意:此命令应该在 外部 homestead 环境中执行。如果您想,您仍然可以使用 npm install

yarn install

安装 bower 依赖

bower install

设置 Laravel,执行这些命令后,请修改 .env 文件以匹配您自己的环境设置

sudo cp .env.example .env
sudo chmod -R 777 storage bootstrap/cache
php artisan key:generate

数据库 & 数据填充

您需要创建一个数据库,例如 opensmarty_starter,编码为 utf8mb4,校对为 utf8mb4_unicode_ci

MySQL 查询

CREATE DATABASE `opensmarty_starter` DEFAULT CHARACTER SET = `utf8mb4` DEFAULT COLLATE = `utf8mb4_unicode_ci`;

.env 文件中更改数据库配置以匹配您刚刚创建的数据库。

在设置好数据库配置后,您现在可以执行迁移和数据填充。

php artisan migrate

php artisan db:seed

使用加密密钥安装 Laravel Passport 以生成安全访问令牌。

php artisan passport:install

当部署到服务器时

sudo chown www-data:www-data storage/oauth-*.key
sudo chmod 600 storage/oauth-*.key

准备就绪

到目前为止,您应该能够访问主页。

例如,如果您设置了域名:http://opensmarty-starter.app/

只需访问:http://opensmarty-starter.app/

它将提示您登录,请使用

opensmarty@163.com
Abc12345

您可以从文件 database/seeds/UsersTableSeeder.php 中更改数据填充账户信息

您完成了。是的!

探索并在此基础上构建。

开发过程和流程

必备知识

在您开始构建之前,您需要了解、阅读和理解以下内容。

我们建议使用 PHPStorm IDE 来构建和开发您的项目。

命名空间

app/ 文件夹的默认命名空间是 Opensmarty

如果您不熟悉 Opensmarty Starter 的命名空间,不建议更改命名空间,因为 Opensmarty 命名空间在整个项目中使用得非常广泛。

但是,如果您了解自己在做什么,仍然可以更改它,因为某些命名空间需要手动更改。

要将命名空间更改为您的首选

php artisan app:namespace YourApp

基础类

为了充分利用 Opensmarty Starter 提供的所有功能,您应该从基础类(如果有)扩展类,例如 BasePresenterBaseController 等。

模型

所有模型都位于 app/Models/ 文件夹下。

app/Models/Foundation/ 文件夹下默认创建的 User 模型具有主键 user_id,您不应该对这个类进行大量修改,因为它在整个项目中几乎到处都在使用。您可以使用全局函数 auth_user() 来访问当前登录的用户。

每个模型都应该扩展 Opensmarty/Models/BaseModel,它有一个观察器 Opensmarty/Observers/BaseModelObserver,您可以使用它来利用模型中的所有模型事件,例如 onCreatingonCreatedonUpdatingonDeleting 等。

创建新模型时,您应该使用命令来自动生成相关的存储库类。

php artisan starter:entity Post

Web HTTP

所有与 Web 相关的文件都位于 app/Http/ 文件夹下。

Web 路由定义在文件 routes/web.php

Web 控制器定义在文件夹 app/Http/Controllers/

RESTful API

所有与 API 相关的文件都位于 app/Api/ 文件夹下。

API 路由定义在文件 routes/api.php

API 控制器定义在文件夹 app/Api/Controllers/

在创建 API 之前,您需要测试它们才能使用。您应该使用提供的单元测试或创建新的单元测试来测试所有 API。不建议使用浏览器或 Postman 等工具进行测试。

仓库模式

所有与存储库相关的文件都位于 app/ 下,具体类型作为父文件夹。

存储库:app/Repositories/

存储库 Eloquent:app/Repositories/Eloquent/

存储库接口:app/Repositories/Interfaces/

存储库标准:app/Repositories/Criteria/

表示者:app/Presenters/

转换器:app/Transformers/

验证器:app/Validators/

资源

Angulr 样式和脚本:resources/assets/angulr/

Angulr 与 Blade 视图:resources/views/angulr/

Vuejs:resources/assets/js/

Less 样式:resources/assets/less/app.less

Sass 样式:resources/assets/sass/app.scss

在脚本、样式中进行更改后,您需要运行命令。

在开发中,运行

npm run dev

在开发和监视资源更改时,运行

npm run watch

在生产中,运行

npm run production

更改主题文件后,运行

npm run theme

API 和单元测试

注意

您可能需要将 dev 环境下的 local 服务配置从 config/rest-client.php 更改为适合您的本地开发配置。

还要将 .env 文件中的 API_TEST_CLIENT_SECRET 的值更改为 oauth_clients 表中 Password Grant Client 记录的 secret 值。

单元测试:tests/

API 单元测试:tests/Api/

调试和 Clockwork

查看文件 storage/clockwork.sqlite 中的所有请求和请求信息

OAuth Web 支持

OAuth HTTP URL:http://opensmarty-starter.app/console/oauth

Web 日志查看器

日志查看器 HTTP URL:http://opensmarty-starter.app/console/logs

数据库

我们推荐使用迁移来更改数据库结构和数据填充。

直接更改数据库结构或不遵循迁移方式强烈不推荐。

创建数据库迁移的流程
  1. 创建迁移文件(当使用 php artisan starter:entity 时自动生成),位于文件夹 database/migrations/

  2. 向迁移文件中添加必要的列(当使用 php artisan starter:entity 时自动生成)

建议使用 tablename_id 格式作为主键自增键,例如,对于 posts 表,您需要使用 post_id,当它成为外键时,您应在其他表中保持相同的名称 post_id

$table->increments('post_id');

还建议在所有表中使用 user_id 字段,例如,在 company_staffs 表中使用 user_id 作为员工 ID 而不是 staff_id。如果某些表中 user_id 没有意义,您仍然应该为了将来使用而保留 user_id 字段。因此,您需要智能地使用它,以适应您的表场景。同样,您可能还希望根据您的需要添加一个 related_user_id 字段,以指示某些记录与用户相关。

以下列始终由 BaseModel 需要

$table->unsignedInteger('user_id')->index();

$table->unsignedInteger('created_by')->nullable();
$table->timestamp('created_at')->nullable();
$table->ipAddress('created_ip')->nullable();
$table->unsignedInteger('updated_by')->nullable();
$table->timestamp('updated_at')->nullable();
$table->ipAddress('updated_ip')->nullable();

并删除 timestamps()

// $table->timestamps();

最终,表的蓝图应如下所示,例如在 posts 表中

$table->increments('post_id');
$table->unsignedInteger('user_id')->index();

// Adding more table related fields here...

$table->unsignedInteger('created_by')->nullable();
$table->timestamp('created_at')->nullable();
$table->ipAddress('created_ip')->nullable();
$table->unsignedInteger('updated_by')->nullable();
$table->timestamp('updated_at')->nullable();
$table->ipAddress('updated_ip')->nullable();
  1. 在文件 database/factories/ModelFactory.php 中添加工厂支持

  2. 在文件夹 database/seeds/ 中创建数据填充支持

刷新数据库迁移和数据填充

当您添加或更改迁移或种子文件,或者您只是想刷新数据库中的所有内容时

php artisan migrate:refresh --seed

许可证

Opensmarty Starter 框架是开源软件,根据 MIT 许可证 许可。