gorankrgovic / larablog
Larablog是一个简单但功能强大的博客包,适用于Laravel 5.6及以上版本。
v1.0.1
2018-06-26 13:26 UTC
Requires
- php: ^7.1.3
- kkszymanowski/traitor: ^0.2.0
Requires (Dev)
- phpunit/phpunit: >=5.4.3
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2024-09-12 09:03:22 UTC
README
一个用于简化博客(或新闻)文章维护的包。
安装、配置和使用
通过Composer
$ composer require gorankrgovic/larablog
配置
安装包后,它应该会被Laravel自动发现。为了检查这一点,在您的终端中简单运行以下命令:
$ php artisan
运行命令后,您应该能够找到所有可用的larablog
命令。
第一步是发布供应商文件。在这种情况下,只有配置文件。
$ php artisan vendor:publish --tag="larablog"
检查配置文件后,这取决于您的技能:)。我建议运行
$ php artisan larablog:setup
上述命令将安装模型,创建迁移文件并向您的User模型添加一个特质。
设置完成后,请运行迁移命令
$ php artisan migrate
这将为此包创建表格:文章和分类。(您可以在配置文件中更改名称)。
一旦表格就绪,请确保初始化分类。
$ php artisan larablog:seeder
上述命令将创建一个seeder类。在创建seeder后,请运行
$ composer du
然后运行seeder
$ php artisan db:seed --class=LarablogSeeder
这就完成了。
用法
创建您的控制器资源或任何其他东西,然后简单地进行以下操作:
$article = new Article([ 'title' => 'This is my title', 'slug' => 'this-is-my-slug', 'excerpt' => 'This is my excerpt', 'content' => 'This is my content', 'is_featured' => false, 'status' => 'publish', 'publish_at' => Carbon::now() ]); // associate the user $article->associateUser(1); // save the article $article->save(); // attach the category $article->attachCategory('uncategorized'); // name or the ID // or multiple categories $article->attachCategories([1, 2, 3]); // names or ID's
例如,要获取文章,您可以使用以下方法:
$article = Article::find(1); // get the categories (cacheable) $article->getCategories();
或者您可以从用户那里获取所有文章
$user = User::find(1); $articles = $user->getArticles(); // or $articles = $user->getArticlesPaginated(20); // or $articles = $user->articles()->get();
您还可以使用提供的范围
$articles = Article::whereCategoriesAre([1, 2, 3])->get(); // pass an array of IDs // or $articles = Article::whereCategoryIs(1)->get(); // pass an id
外观
Larablog为您提供方便的辅助工具,使您的生活更加轻松
use Gorankrgovic\Larablog\Facades\Larablog; // ... and then in your methods... // to generate a unique slug which will append number at the end of the slug if the same slug exists $slug = Larablog::slug($title); // to convert string to paragraph delimited text $text = Larablog::autop($content); // to generate the excerpt $excerpt = Larablog::excerpt($content); // to just sanitize the title and to get a slug $slug = Larablog::sanitize_title($title);
基本上就是这些...