我想在Laravel中创建一个API第一个应用程序.我不知道这样做的最佳方法是什么,我会解释我想要做什么,但请随意以不同的方式给出答案.
我不希望我的所有前端都用javascript编写,并用angular.js或类似的东西解析API的JSON输出.我希望我的Laravel应用程序生成HTML视图.我正试图让两个控制器一个用于API,另一个用于网络.对于show User动作,我的routes.php如下所示:
# the web controller
Route::controller('user', 'WebUserController');
# the api controller
Route::group(array('prefix' => 'api'), function() {
Route::resource('user', 'UserController');
});
Run Code Online (Sandbox Code Playgroud)
所以/user
将带我去WebUserController
,/api/user
并带我去UserController
.现在我想将所有逻辑放在API中UserController
,并从中调用它的动作WebUserController
.以下是两者的代码:
class UserController extends BaseController
{
public function show($id)
{
$user = User::find($id);
return Response::json(array('success'=>true,'user'=>$user->toArray()));
}
}
class WebUserController extends UserController
{
public function getView($id)
{
# call the show method of the API's User Controller
$response = $this->show($id);
return View::make('user.view')->with('data', $response->getData());
}
}
Run Code Online (Sandbox Code Playgroud)
在WebUserController
我能够获得响应的json内容 …
我只能访问S3存储桶中的特定目录.
例如,s3cmd
如果我尝试列出整个存储桶,则使用该命令:
$ s3cmd ls s3://my-bucket-url
Run Code Online (Sandbox Code Playgroud)
我收到一个错误: Access to bucket 'my-bucket-url' was denied
但是如果我尝试访问存储桶中的特定目录,我可以看到内容:
$ s3cmd ls s3://my-bucket-url/dir-in-bucket
Run Code Online (Sandbox Code Playgroud)
现在我想用python boto连接到S3存储桶.与以下相似:
bucket = conn.get_bucket('my-bucket-url')
Run Code Online (Sandbox Code Playgroud)
我收到一个错误: boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
但如果我尝试:
bucket = conn.get_bucket('my-bucket-url/dir-in-bucket')
Run Code Online (Sandbox Code Playgroud)
脚本停顿大约10秒钟,然后打印出错误.贝娄是完整的痕迹.知道如何继续这个吗?
Traceback (most recent call last):
File "test_s3.py", line 7, in <module>
bucket = conn.get_bucket('my-bucket-url/dir-name')
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 471, in get_bucket
return self.head_bucket(bucket_name, headers=headers)
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 490, in head_bucket
response = self.make_request('HEAD', bucket_name, headers=headers)
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 633, in make_request
retry_handler=retry_handler
File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 1046, …
Run Code Online (Sandbox Code Playgroud) 我有一个简单的 HTML 表单,它通过 AJAX 将选定的文件发送到 API 端点。
如果我执行以下步骤:
Chrome 将失败第二个请求并显示错误:net::ERR_UPLOAD_FILE_CHANGED
。请注意,如果您在初始上传之前更改文件,则上传文件将没有问题。该错误仅在第二次上传时发生,当您在首次成功上传后更改文件时。我正在用 CSV 文件对此进行测试,并在文本编辑器中更改它们。
似乎没有办法隔离该错误。
有没有办法解决?
如果没有,是否可以捕获此特定错误,并向用户显示有意义的消息。Fetch 在承诺中返回的错误没有关于此的具体信息。我看到的唯一地方ERR_UPLOAD_FILE_CHANGED
是在浏览器开发控制台中。
我很确定大约一年前(2019 年初)没有任何问题,因为可以重新上传更改的文件,在我们的 UI 流程中很好地发挥了作用。现在我们需要强制用户再次选择文件。所以我假设这是在最近的 chrome 更新中引入的。
这是代码的简化片段:
<html>
<head>
<script type='text/javascript'>
document.addEventListener("DOMContentLoaded", function(){
const button = document.querySelector('#btnSubmit');
button.addEventListener('click', () => {
const form = new FormData(document.querySelector('#theForm'));
const url = '/my-api'
const request = new Request(url, {
method: 'POST',
body: form
});
fetch(request).then(function() {
console.log("ok");
}).catch(function(err) {
console.log(err);
});
});
});
</script>
</head>
<body> …
Run Code Online (Sandbox Code Playgroud) 有没有办法列出清漆缓存存储的内容?此外,以某种方式列出最常见的缓存命中将是很好的.
我找到了一种方法,通过列出发送到后端的内容来查看最常见的缓存未命中:
varnishtop -b -i TxURL
Run Code Online (Sandbox Code Playgroud)
查看我的顶级缓存命中URL是非常有用的.
编辑:我正在使用版本:varnish-3.0.3修订版9e6a70f
我试图通过添加基于用户输入列表的Q对象在Django中构建一个复杂的查询:
from django.db.models import Q
q = Q()
expressions = [
{'operator': 'or', 'field': 'f1', 'value': 1},
{'operator': 'or', 'field': 'f2', 'value': 2},
{'operator': 'and', 'field': 'f3', 'value': 3},
{'operator': 'or', 'field': 'f4', 'value': 4},
]
for item in expressions:
if item['operator'] == 'and':
q.add(Q(**{item['field']:item['value']}), Q.AND )
elif item['operator'] == 'or':
q.add(Q(**{item['field']:item['value']}), Q.OR )
Run Code Online (Sandbox Code Playgroud)
基于此,我期望得到以下条件的查询:
f1 = 1 or f2 = 2 and f3 = 3 or f4 = 4
Run Code Online (Sandbox Code Playgroud)
其中,基于默认运算符优先级将执行为
f1 = 1 or (f2 = 2 and f3 …
Run Code Online (Sandbox Code Playgroud) 有没有办法从谷歌地图获得每个城市的所有社区.
例如,下面的API调用告诉我,小意大利是纽约曼哈顿的一个社区.
http://maps.google.com/maps/api/geocode/json?address=Little+Italy
有没有办法列出属于纽约或任何其他美国城市的所有此类社区项目.
基本上我需要刮一些嵌套标签的文本.
像这样的东西:
<div id='theNode'>
This is an <span style="color:red">example</span> <b>bolded</b> text
</div>
Run Code Online (Sandbox Code Playgroud)
我想要一个表达式来产生这个:
This is an example bolded text
Run Code Online (Sandbox Code Playgroud)
我一直在努力工作一小时或更长时间没有结果.
任何帮助表示赞赏
我有nginx设置为反向代理服务器,我想删除备份服务器上设置的某些cookie(apache)
我的网站使用了很多我无法控制的cookie(Expression Engine CMS,不要问我为什么).我想删除一些cookie(比如饼干AB和C)并保留其他一些(cookies D和E).
之后,我将设置nginx以仅在请求没有cookie时才响应缓存内容.
你知道怎么做吗?谢谢
到目前为止,我的配置:
proxy_cache_path /opt/nginx/cache levels=1:2 keys_zone=mycache:20m max_size=1G;
proxy_temp_path /opt/nginx/tmp_cache/;
proxy_ignore_headers Expires Cache-Control Set-Cookie;
proxy_cache_use_stale error timeout invalid_header http_502;
proxy_cache_bypass $cookie_nocache;
proxy_no_cache $cookie_nocache;
Run Code Online (Sandbox Code Playgroud)
...
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache mycache;
proxy_cache_valid 200 302 6h;
proxy_cache_valid 404 1m;
proxy_pass http://x.x.x.x:8080;
}
Run Code Online (Sandbox Code Playgroud) 我想从分配给的所有组中删除用户.我在Django中使用标准的auth应用程序.
到目前为止,我可以一次删除一个组:
user.groups.remove(group)
Run Code Online (Sandbox Code Playgroud)
但这增加了很多sql开销.我知道这是一个多对多关系,但我无法找到哪个模型代表多对多映射并从那里调用delete方法.
我想用Django ORM执行以下查询:
delete from auth_user_group where user_id = 123
Run Code Online (Sandbox Code Playgroud) 这不是一个特定的问题,更多的是一般的疑惑.
当您必须以1:M关系对多个表进行删除时,最好是使用级联删除制作FK约束还是连接delete语句中的表.
我有一个旧项目,它有相关表的单独删除语句,有些语句没有执行,数据完整性受到损害.我不得不在两者之间做出决定,所以我想了一下什么是更好的解决方案.
还可以选择进行存储过程或事务.
所以我正在寻找意见或建议......?
django ×2
sql ×2
ajax ×1
amazon-s3 ×1
api ×1
boto ×1
controller ×1
cookies ×1
django-orm ×1
file-upload ×1
google-maps ×1
javascript ×1
laravel ×1
mysql ×1
nginx ×1
orm ×1
php ×1
python ×1
rdbms ×1
rest ×1
varnish ×1
xpath ×1