我知道有人讨论 Symfony2 中处理路由的最佳实践(routing.yml 与注释)。让我提一下,我想保持原样,使用注释。
当我在控制器中为单个操作定义多个路由时,注释的最后一个定义似乎@Method覆盖了所有其他定义,这就是为什么我收到以下错误:
No route found for "POST /index": Method Not Allowed (Allow: GET, HEAD)
这只是我正在使用的一小段代码。
namespace MySelf\MyBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class MyController extends Controller{
/**
* @Route(
* "/index",
* name="index_default"
* )
* @Method({"GET", "POST"})
*
* @Route(
* "/index/{id}",
* name="index",
* requirements={
* "id": "\d+"
* }
* )
* @Method({"GET"})
*
* @return Response
*/
public function indexAction($id = null){
/*DO SOME FANCY STUFF*/
...
return $response;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Ajax 执行 DELETE 请求,但它不起作用,我收到内部错误,但我看不到问题,你能帮助我吗?
这是javascript的部分代码:
$.ajax({
url: 'http://localhost:8080/actors/remover',
type: 'DELETE',
data: JSON.stringify(movie),
traditional:true,
dataType: 'json',
success: function(result) {...},
error: function(result){...}
});
Run Code Online (Sandbox Code Playgroud)
这是我的控制器的代码:
@RequestMapping(value = "/actors/remover", method = RequestMethod.DELETE)//TODO, elimina un attore dal db
public boolean remove(@PathVariable("movie") int movie) {
System.out.println("Attori da cancellare");
serv.deleteActors(movie);
return true;
}//remove
Run Code Online (Sandbox Code Playgroud) 我试图了解通过渲染 JSON 异步更新 Rails 视图的机制(因为我的老板希望这样做)。
到目前为止还不是很成功。
有没有办法检查操作系统层上看到的响应?就像我使用时一样curl?我正在尝试将我的学习成果写成一篇有关该主题的博客文章,并且有一种方法可以可视化 Rails 在渲染 JSON 时发出的内容,这将非常有帮助。
重要的控制器部分如下所示:
def create
@order = Order.find_by(id: params[:order_id])
@comment = current_user.comments.new(comment_params)
.
.
return unless @comment.save!
respond_to do |format|
format.json { render json: @comment, context: self }
end
end
Run Code Online (Sandbox Code Playgroud)
编辑:
根据我尝试使用调试器/撬来检查响应的评论之一,首先确保 @comment 对象包含数据:
(byebug) @comment
#<Comment id: 3090, order_id: 125, user_id: 18, content: "asdfad", created_at: "2017-02-01 12:21:25", updated_at: "2017-02-01 12:21:25">
Run Code Online (Sandbox Code Playgroud)
看起来不错。
(byebug) response.body
""
Run Code Online (Sandbox Code Playgroud)
不太酷,JSON 数据在哪里?为什么身体是空的?
我正在尝试建立一个简单的多对多关系,目标是将朋友添加到数据库中。
我正在尝试使用我的控制器添加朋友,但我找不到使用 ID 获取用户实体的方法。
按照文档中的示例:
/**
* @Entity
*/
class User
{
/**
* @Id
* @Column(type="integer")
*/
private $id;
/**
* @ManyToMany(targetEntity="User")
* @JoinTable(name="friends",
* joinColumns={@JoinColumn(name="user_a_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="user_b_id", referencedColumnName="id")}
* )
* @var \Doctrine\Common\Collections\ArrayCollection
*/
private $friends;
/**
* Constructor.
*/
public function __construct()
{
$this->friends = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @return array
*/
public function getFriends()
{
return $this->friends->toArray();
}
/**
* @param User $user
* @return void
*/
public function addFriend(User $user)
{
if (!$this->friends->contains($user)) …Run Code Online (Sandbox Code Playgroud) 要将控制器标记为需要授权,通常可以像这样装饰它:
[Authorize]
public class MyController : Controller
Run Code Online (Sandbox Code Playgroud)
我们的身份验证是通过第三方提供商进行的,并且考虑到它的设置方式,我们只希望它在我们的生产环境中实际生效,我们不希望它在 QA 环境中处于活动状态。在 Startup.cs 文件中关闭环境很容易,但是有没有办法有条件地装饰控制器?我开始研究政策和角色,看起来可能会被黑客入侵,但有更好的方法吗?
我正在使用 slim 3 构建控制器/中间件,我希望从附加到一个组的中间件中将一些数据传递到我的控制器 - 操作中的 $args 参数。
这是一些代码:
class MyController
{
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function index(Request $request, Response $response, $args) {
return $this->container->get('renderer')->render($response, "index.html.twig", $args);
}
}
Run Code Online (Sandbox Code Playgroud)
class MyMiddleware
{
public function __invoke(Request $request, Response $response, $next)
{
// do some stuff to inject further down to $args some data
return $next($request, $response);
}
}
Run Code Online (Sandbox Code Playgroud)
$app->group('/group', function () use ($app){
//routes
})->add(new MyMiddleware());
Run Code Online (Sandbox Code Playgroud)
我的用例是将内容发送到由这些控制器的操作呈现的所有视图,因此我也可以使用其他方法来执行此操作:)
谢谢。
我创建了一个shopware6插件,其中包含一个前端自定义路由,该路由应该能够接收来自 ExactOnline 的 POST 请求。
但是当我用 Postman 进行一些测试时,我收到“ 405 Method Not allowed ”。这是我的控制器:
<?php
namespace Emakers\TransmissionPlugin\Controller;
//use Emakers\TransmissionPlugin\Controller\Frontend\Services\ExactRequirements;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class AccountsController extends StorefrontController
{
/**
* @Route("/accounts", name="frontend.accounts", options={"seo"="false"}, methods={"POST"})
*/
public function accounts()
{
die('ok boy');
$file = 'custom/plugins/TransmissionPlugin/Resources/webhooks/accountWebhook.txt';
}
}
Run Code Online (Sandbox Code Playgroud)
当我用methods={"GET"}替换methods = {"POST"}时,相同的测试请求返回“ok boy”,这是正常的。
我已经在Shopware5中创建了一个这样的插件,并且GET和POST请求已经可以正常工作,无需执行任何特殊操作。
在我的情况下,如何在Shopware6上发出ALLOW POST请求?
谢谢 !
我是一名 Android 开发者,也是 Flutter 新手。我真的很喜欢 Mobx 的工作方式,因为它记住了 Android 的 ViewModel。顺便说一句,当我创建 ViewModel 时,我喜欢将存储库作为参数来创建它,这样我就可以使用不同的数据源(即本地或云)来测试它。
所以,这就是我现在的课。
import 'package:mobx/mobx.dart';
part 'create_bill_controller.g.dart';
class CreateBillController = _CreateBillControllerBase
with _$CreateBillController;
abstract class _CreateBillControllerBase with Store {
final appBarTitle = 'Criar Conta';
final criarConta = 'Criar conta';
final nomeDaConta = 'Nome da conta';
final seuNome = 'Seu nome';
@action
createBill(String billname, String userName) {
// here, dataSource should be given in a constructor
datasource.createBill(billName, userName);
}
}
Run Code Online (Sandbox Code Playgroud)
如何将数据源(存储库)作为参数传递给此类?
我有 2 个具有不同 viewportFraction 的 PageView。有没有办法滚动其中一个 PageView,而另一个 PageView 则在同一页面或偏移上滚动?
另外问一下,是否可以在代码中控制PageView到偏移量的中间?
class MyPageControllers extends StatefulWidget {
@override
_MyPageControllersState createState() => _MyPageControllersState();
}
class _MyPageControllersState extends State<MyPageControllers> {
PageController _controller1;
PageController _controller2;
Widget _itemBuilder(BuildContext context, int index) => Container(
color: Colors.primaries[index % Colors.primaries.length],
child: Center(
child: Text(
index.toString(),
style: TextStyle(color: Colors.white, fontSize: 60),
),
),
);
@override
void initState() {
super.initState();
_controller1 = PageController(viewportFraction: 0.8);
_controller2 = PageController(viewportFraction: 0.5);
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: PageView.builder( …Run Code Online (Sandbox Code Playgroud) 您好,我正在尝试使用react-hook-form 和material-ui 制作一种形式。我不想每次都为所有文本字段编写控制器。因此,我在另一个文件中声明它并以我的表单调用它,但它不起作用,我不明白为什么,因为在我观看的一些视频中它起作用。有什么问题以及如何解决它?
表单字段
import React from 'react'
import { TextField, Grid } from '@material-ui/core'
import { useForm, Controller, useFormContext } from 'react-hook-form'
const FormField = ({name, label}) => {
const { control } = useForm()
return (
<Grid item xs={12} sm={6} >
<Controller
render = {({field}) =>
<TextField
{...field}
label={label} required
/>}
name={name}
control = {control}
defaultValue=""
/>
</Grid>
)
}
export default FormField
Run Code Online (Sandbox Code Playgroud)
地址表
import React from 'react'
import { InputLabel, Select, MenuItem, Button, Grid, Typography, TextField } …Run Code Online (Sandbox Code Playgroud) controller ×10
php ×3
ajax ×2
flutter ×2
action ×1
annotations ×1
asp.net ×1
c# ×1
constructor ×1
entity ×1
javascript ×1
jquery ×1
json ×1
material-ui ×1
middleware ×1
mobx ×1
parameters ×1
plugins ×1
post ×1
react-hooks ×1
reactjs ×1
shopware ×1
slim-3 ×1
symfony ×1
symfony4 ×1