linna/app

Linna 应用程序

维护者

详细信息

github.com/linna/app

主页

源代码

问题

安装: 35

依赖项: 0

建议者: 0

安全: 0

星标: 6

关注者: 4

分叉: 3

开放问题: 1

类型:项目

v0.16.1 2022-10-03 20:47 UTC

README

Linna Logo
Linna dotenv Logo

Tests PDS Skeleton PHP 8.1

关于

Linna 框架的应用程序骨架

实际稳定环境

  • app 0.16.0
  • 框架 0.27.0
  • auth-mapper-* 0.2.0

索引

  1. 要求
  2. 安装
  3. 首次运行前
  4. URL 重写
  5. Dot env 文件

要求

注意:应用程序仅在 Linux 系统下使用 Apache 服务器和默认 php.ini 配置下进行了测试

安装

注意:如果需要管理员权限,请考虑使用 sudo 命令,并不要忘记设置适当的文件夹权限

使用 composer

cd /var/www/html
mkdir app
composer create-project --prefer-dist linna/app app

其中 app 是位于 web 服务器文档根目录下的目录,例如 /var/www/html/app

之后,运行 composer dump-autoload 以优化文件自动加载

composer dump-autoload --optimize

首次运行前

在位于 /var/www/html/app/config 目录中的 config.php 文件中更改配置。

协议和应用程序目录配置

$options = [

    'app' => [
        //protocol utilized [http://, https://]
        //default value set automatically
        'protocol'     => REQUEST_SCHEME.'://',
        //folder of the app, if app isn't in the web server root add a
        //directory (/app, /other/app) else insert a / (slash) as value
        //default value [/app]
        'subFolder'    => '/app',
        //public folder of the app, starting from web server root
        //default value [/app/public]
        'publicFolder' => '/app/public',
        //.env file position, remember to add ../ if don't use an absolute path
        'envFile'      => '../.env',
        //name of the fallback route, indicate the path when router return a NullRoute
        //default /error/404
        'onNullRoute'  => '/error/404'
    ],

    //other options
];

路由配置

$options = [

    //other options

    'router' => [
        //must be equal to app.subFolder, it represents the part of the path
        //that the router ignore when check a route. Example '/app/user/delete/5'
        //become '/user/delete/5' where the router subtract the basePath
        //default [/app]
        'basePath'             => '/app',
        //url rewriting
        //default [true]
        'rewriteMode'          => true,
        //part of the url that the router ignore when url rewriting is off
        'rewriteModeOffRouter' => '/index.php?',
    ],

    //other options
];

URL 重写

如果启用了 config.php 中名为 rewriteMode 的路由器选项,则需要配置您的虚拟主机/服务器块。

为 mod_rewrite 的 Apache 虚拟主机配置

有关 Apache VirtualHost 配置,请参阅
https://httpd.apache.ac.cn/docs/current/vhosts/
有关 Apache mod_rewrite 配置,请参阅
https://httpd.apache.ac.cn/docs/current/rewrite/

<VirtualHost *:80>

    # Other virtual host directives.

    <Directory /var/www/html/app>
        RewriteEngine on
        # Route to /app/public
        RewriteRule ^(.*)  public/$1 [L]
    </Directory>

    <Directory /var/www/html/app/public>
        # Necessary to prevent problems when using a controller named "index" and having a root index.php
        # more here: https://httpd.apache.ac.cn/docs/current/content-negotiation.html
        Options -MultiViews

        # Activates URL rewriting (like myproject.com/controller/action/1/2/3)
        RewriteEngine On

        # Prevent people from looking directly into folders
        Options -Indexes

        # If the following conditions are true, then rewrite the URL:
        # If the requested filename is not a directory,
        RewriteCond %{REQUEST_FILENAME} !-d
        # and if the requested filename is not a regular file that exists,
        RewriteCond %{REQUEST_FILENAME} !-f
        # and if the requested filename is not a symbolic link,
        RewriteCond %{REQUEST_FILENAME} !-l

        # then rewrite the URL in the following way:
        # Take the whole request filename and provide it as the value of a
        # "url" query parameter to index.php. Append any query string from
        # the original URL as further query parameters (QSA), and stop
        # processing (L).
        # https://httpd.apache.ac.cn/docs/current/rewrite/flags.html#flag_qsa
        # https://httpd.apache.ac.cn/docs/current/rewrite/flags.html#flag_l
        RewriteRule ^(.+)$ index.php [QSA,L]
    </Directory>

    # Other virtual host directives.

</VirtualHost>

为 mod_rewrite 的 Apache .htaccess 配置

如果您无法访问 Apache 虚拟主机配置,可以在应用程序中添加 .htaccess 文件以启用 mod_rewrite。

app/ 目录中创建 .htaccess 文件,内容如下

RewriteEngine on
# Route to /app/public
RewriteRule ^(.*)  public/$1 [L]

app/public/ 目录中创建 .htaccess 文件,内容如下

# Necessary to prevent problems when using a controller named "index" and having a root index.php
# more here: https://httpd.apache.ac.cn/docs/current/content-negotiation.html
Options -MultiViews

# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On

# Prevent people from looking directly into folders
Options -Indexes

# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l

# then rewrite the URL in the following way:
# Take the whole request filename and provide it as the value of a
# "url" query parameter to index.php. Append any query string from
# the original URL as further query parameters (QSA), and stop
# processing (L).
# https://httpd.apache.ac.cn/docs/current/rewrite/flags.html#flag_qsa
# https://httpd.apache.ac.cn/docs/current/rewrite/flags.html#flag_l
RewriteRule ^(.+)$ index.php [QSA,L]

Nginx

有关 Nginx 服务器块配置,请参阅
https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/

使用 Nginx 设置 URL 重写比 Apache 对应版本简单,将 try_files $uri $uri/ /index.php?$args; 添加到 location 块中

server {

    # Other directives
    
    location / {
        # Url rewrite
        # Add line blow to location block for enable url rewriting
        try_files $uri $uri/ /index.php?$args;
    }

    # Other directives
}

Dot env 文件

使用 composer 安装时,会在 app 根目录中创建一个 .env 文件,可以用于声明默认环境变量。

.env 文件内容如下

# Session
session.name   = linna_session
session.expire = 1800

## Pdo Mysql
pdo_mysql.user     = root
pdo_mysql.password =

## Mysqli
#mysqli.host     = 127.0.0.1
#mysqli.user     = root
#mysqli.password =
#mysqli.database = linna_db
#mysqli.port     = 3306

## MongoDB
#mongo_db.uri = mongodb://localhost:27017

## Memcached
#memcached.host = localhost
#memcached.port = 11211

.env 文件有效键

session.name
session.expire

pdo_mysql.dsn
pdo_mysql.user
pdo_mysql.password

pdo_pgsql.dsn
pdo_pgsql.user
pdo_pgsql.password

mysqli.host
mysqli.user
mysqli.password
mysqli.database
mysqli.port

mongo_db.uri

memcached.host
memcached.port

文件中声明的值将覆盖 config.php 的值。

可以通过编辑 envFile 的值来更改 .env 文件的位置。

$options = [

    'app' => [
        //other app options
        'envFile'           => '../.env'
    ],

    //other options
];

如果您不想使用 .env 文件,可以删除它。