watson/active
Composer 安装命令:
composer require watson/active
包简介
Laravel helper for recognising the current route, controller and action
README 文档
README
Active is a helper package for Laravel that makes it easy to recognize the current path or route, useful for adding 'active' classes (like those used in the Boostrap framework) and performing other actions only when a certain route is active. It also includes helpers for retrieving the current controller and action names.
Installation
First, simply require the package through Composer.
composer require watson/active
Using Laravel 5.1? The latest version of the package that will work for you is 2.0.4.
Next, add the service provider in your config/app.php file.
Watson\Active\ActiveServiceProvider::class
If you'd like to use the Facade instead of the helper functions, add it to the aliases array.
'Active' => Watson\Active\Facades\Active::class
Using Active
Helper functions
Active ships with a couple of helper functions which make it easy to use without the facade or creating an instance of your own.
active()
is_active()
Using active()
You pass an array of routes or paths you want to see are the current page, and if any match this function will return the string active, for Bootstrap. Alternatively, you can pass a custom return string as the second argument.
active(['login', 'users/*', 'posts.*', 'pages.contact']); // Returns 'active' if the current route matches any path or route name. active(['login', 'logout'], 'active-class'); // Returns 'active-class' if the current route is 'login' or 'logout'. active(['login', 'logout'], 'active-class', 'fallback-class'); // Returns 'fallback-class' if the current route is not 'login' or 'logout'.
In the first example, the function will return the string active if the current path is login, starts with users/ or if the name of the current route is posts.create.
Do note that a number of argument types are provided: you can use a path string, you can use a path string with a wildcard (using the *) and you can also use named routes.
You can use this function with your links to give them an active state.
<a href="{{ route('posts.index') }}" class="{{ active('posts.index') }}">All posts</a>
You can also provide certain paths or routes to be exluded when being considered.
active(['pages/*', 'not:pages/contact']) active(['pages.*', 'not:pages.contact'])
Using is_active()
This works much the same as active(), you can pass the paths and routes to it but instead it will return a boolean if the current page matches.
@if (is_active('posts/*')) You're looking at a blog post! @endif
Additional helpers
Two additional functions are provided to get the current controller and action, if your routing is being handled by a controller for a request. These functions will return the lowercase controller/action name, without the method of the request. Here is an example for a request that is routed to `FooController@getBar':
$controller = controller_name(); // foo $action = action_name(); // bar
Licence
Active for Laravel is 100% free and open-source, under the MIT license. Use it however you want.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
watson/active 适用场景与选型建议
watson/active 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.74M 次下载、GitHub Stars 达 326, 最近一次更新时间为 2014 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「laravel」 「active」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 watson/active 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 watson/active 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 watson/active 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
A Symfony extension to get active class base on current bundle/controller/action
LDAP user provider bundle for Symfony 6.4
The helper class for Laravel applications to get active class base on current route for Laravel 11
Write down your routing mapping at one place
Anax Database Active Record module for model classes.
统计信息
- 总下载量: 3.74M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 336
- 点击次数: 23
- 依赖项目数: 16
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-03-06