有没有办法在路由中间添加可选参数?
示例路线:
/things/entities/
/things/1/entities/
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它不起作用:
get('things/{id?}/entities', 'MyController@doSomething');
Run Code Online (Sandbox Code Playgroud)
我知道我能做到这一点......
get('things/entities', 'MyController@doSomething');
get('things/{id}/entities', 'MyController@doSomething');
Run Code Online (Sandbox Code Playgroud)
...但我的问题是: 我可以在路线中间添加可选参数吗?
假设我有这个架构:
create table rental (
id integer,
rental_date timestamp,
customer_id smallint,
return_date timestamp,
);
Run Code Online (Sandbox Code Playgroud)
运行此查询返回奇怪的结果:
select customer_id, avg(return_date - rental_date) as "avg"
from rental
group by customer_id
order by "avg" DESC
Run Code Online (Sandbox Code Playgroud)
它显示:
customer_id|avg_rent_duration |
-----------|----------------------|
315| 6 days 14:13:22.5|
187|5 days 34:58:38.571428|
321|5 days 32:56:32.727273|
539|5 days 31:39:57.272727|
436| 5 days 31:09:46|
532|5 days 30:59:34.838709|
427| 5 days 29:27:05|
555|5 days 26:48:35.294118|
...
599 rows
Run Code Online (Sandbox Code Playgroud)
为什么会出现像值5 days 34:58:38
,5 days 32:56:32
等等?我以为一天只有24小时在那儿,也许我错了。
演示在这里:http : //sqlfiddle.com/#!17/caa7a/1/0
样本数据: …
我正在使用HTMLPurifier
来清理HTML字符串(它是关于安全性的).
调用HTMLPurifier时会删除某些属性(如width
或height
).我不认为这是一个安全问题.
如何在不重新定义白名单的情况下添加此属性?
我搜索了Stackoverflow和HTMLPurifier文档,但唯一的解决方案似乎是:
$config->set('HTML.Allowed', 'p,b,a[href],i');
Run Code Online (Sandbox Code Playgroud)
但这不是一个解决方案,因为我不想重新定义白名单(我相信默认的HTMLPurifier配置,我只想添加一个例外).
我可以显示如下目录列表:
use std::fs;
fn main() {
let paths = fs::read_dir("./").unwrap();
for path in paths {
println!("Name: {}", path.unwrap().path().display())
}
}
Run Code Online (Sandbox Code Playgroud)
我可以在ReadDir
迭代之前对迭代器进行排序吗?目录名称是类似日期的数字201610131503
.我阅读了文档,ReadDir
但是没有找到内置函数.也许我不知道如何搜索?
使用Laravel,我可以获得客户端IP request()->ip()
.
是否有Laravel内置方式来获取服务器IP?或者它是"不可能的"(与SERVER_ADDR
可靠性相同的问题)
我必须对凌乱的数据库做一些查询.有些列填充了null
空字符串或空字符串.我可以这样查询:
select * from a where b is not null and b <> '';
Run Code Online (Sandbox Code Playgroud)
但这种情况有没有捷径?(匹配每个"非空"值)类似于:
select * from a where b is filled;
Run Code Online (Sandbox Code Playgroud) 我有一个var : $a
. 我不知道它是什么.我想检查一下是否可以算数.通常,只有数组,我可以这样做:
if (is_array($a)) {
echo count($a);
}
Run Code Online (Sandbox Code Playgroud)
但其他一些事情是可数的.让我们说Illuminate\Support\Collection
Laravel是可数的:
if ($a instanceof \Illuminate\Support\Collection) {
echo count($a);
}
Run Code Online (Sandbox Code Playgroud)
但是有两件事情在一起做(并且可能与其他一些可数实例一起工作).就像是:
if (is_countable($a)) {
echo count($a);
}
Run Code Online (Sandbox Code Playgroud)
这种功能存在吗?我错过了什么?
使用Laravel 5,我希望Log::error
通过直接调用此方法或默认ExceptionHandler
调用report
方法,每次(或类似)触发通知(松弛但无关紧要).我想我必须扩展默认的Laravel的Log系统,但我不确定.这样做的(最佳)"laravel方式"是什么?(不改变Log::error
我整个代码中的每个调用).
首先,我认为我只需要将Log Facade更改为另一个,但它不会处理ExceptionHandler
(即由于未捕获的异常导致的500个错误).另一种解决方案可能是直接在添加一些代码ExceptionHandler
,但如果我报告错误,否则它将不会被触发Log::error()
或其他方式(app('logger')->error()
,logger()->error()
,等).
我想写一个打印"OK"然后在方法中返回self的宏.这是我的第一个宏,所以我尝试了这个,认为它只会像文本替换一样,但它失败了:
macro_rules! print_ok_and_return_self {
() => {
println!("OK");
self
}
}
fn main() {
let a = A{};
a.a().a();
}
struct A {}
impl A {
fn a(self) -> Self {
print_ok_and_return_self!()
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
error: macro expansion ignores token `self` and any following
--> src/main.rs:4:13
|
4 | self
| ^^^^
|
note: caused by the macro expansion here; the usage of `print_ok_and_return_self!` is likely invalid in expression context
--> src/main.rs:17:13
|
17| print_ok_and_return_self!()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
快速查看文档后,我知道这不仅仅是文本替换,但我仍然不知道如何使其工作.
引用Laravel文档:
默认情况下,Laravel的基本控制器类使用ValidatesRequests特性,该特性提供了一种方便的方法来使用各种强大的验证规则来验证传入的HTTP请求
的确,阅读代码App\Http\Controllers\Controller
实际上使用了ValidatesRequests
特征。并ValidatesRequests
有一种validate
方法。
我真正感到奇怪的是,文档中的其他所有地方,该validate
方法都是在$request
对象上调用的。它以这种方式工作。我可以使用以下代码验证表单:
public function store()
{
$attributes = request()->validate([
'name' => 'required|string|max:255',
]);
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是我看不到Request类上存在任何validate方法。文件开头只是一个奇怪的注释行:
/**
* @method array validate(array $rules, array $messages = [], array $customAttributes = [])
*/
Run Code Online (Sandbox Code Playgroud)
因此,有两件事:
$request
对象上进行。我的实际问题是:
如果我validate
通过$request
对象使用方法,从文档粘贴的初始引号是否仍然正确?如果是这样,它如何运作?
laravel ×5
php ×4
laravel-5 ×3
postgresql ×2
rust ×2
arrays ×1
html ×1
htmlpurifier ×1
intervals ×1
iterator ×1
laravel-5.5 ×1
rust-macros ×1
sanitization ×1
security ×1
sql ×1