simple-crud / extra-fields
simple-crud 包的扩展字段
v2.0.0
2019-08-24 23:12 UTC
Requires
- php: >=7.2
- cocur/slugify: ^3.0
- psr/http-message: ^1.0
- simple-crud/simple-crud: ^7.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.11
- phpunit/phpunit: ^8.0
- zendframework/zend-diactoros: ^1.3
README
包含扩展字段的 simple-crud 包
安装
此包可以通过 Composer 以 simple-crud/extra-fields 的方式安装和自动加载。
$ composer require simple-crud/extra-fields
文件
用于将文件上传到目录并保存文件名到数据库。检测 Psr\Http\Message\UploadedFileInterface
实例(见此处 PSR-7 标准)。
- 首先,您必须使用属性
SimpleCrud::ATTR_UPLOADS
定义数据库使用的上传路径。 - 在注册字段时,如果字段名为 "file" 或以 "File" 结尾(例如:imageFile, avatarFile 等),则将分配 "File" 格式。
- 默认情况下,文件将保存在名为
[table]/[field]
的子目录中。例如,表user
中字段avatar
的图像将保存在文件夹uploads/user/avatar
中。 - 文件名将被转换为小写并转换为 slug 格式。例如,文件
My Picture.JPG
将重命名为my-picture.jpg
。
use SimpleCrud\Fields\File; //Register the field File::register($simpleCrud); //Configure the directory to upload the files $simpleCrud->setConfig(File::CONFIG_UPLOADS_PATH, '/path/to/uploads'); //You can also configure the File field $simpleCrud->user->file ->setConfig('directory', '/path/to/uploads') //custom directory used instead the default File::DIRECTORY ->setConfig('relative_directory', '/images') //custom subdirectory (by default is /{table_name}/{field_name}) ->setConfig('save_relative_directory', true) //whether save the relative_directory in the database instead only the filename (false by default) //Get the data from the serverRequest $data = $request->getParsedBody(); $files = $request->getUploadedFiles(); //Create the new user $user = $simpleCrud->user->create([ 'name' => $data['name'], 'email' => $data['email'], 'file' => $files['avatar'], ]); //Save the data $user->save(); //Get the avatar file echo $user->file; // /user/avatar/image.jpg;
Slug
用于使用 cocur/slugify 保存 slug 化的值。在注册字段时,如果字段名为 "slug",则将分配 "Slug" 格式。
use SimpleCrud\Fields\Slug; //Register the field Slug::register($simpleCrud); //Create the new article $title = 'Hello world' $article = $simpleCrud->articles->create([ 'title' => $title, 'slug' => $title, ]); //Save the data $article->save(); //Get the slug echo $user->article->slug // hello-world