通过编辑 config/packages/framework.yaml 启用 php 引擎后,我现在面临“模板 ::/test/test.html.php”不退出”错误!
控制器 :
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class TestController extends Controller
{
/**
* @Route("/test.html.php", name="testphp")
*/
public function testphp(){
return $this->render('test/test.html.php');
}
}
Run Code Online (Sandbox Code Playgroud)
路径:Templates/test/test.html.php
我最近在 symfony 4 中启动了一个项目,所以我想制作一个登录页面,我实际上遵循其中的所有安全流程=> https://symfony.com/doc/current/security.html#create-user-class
但是所有本教程都是基于我的项目中不存在的 security.yaml 文件,当我手动添加它时,我遇到了错误,我的应用程序有点冻结......所以我应该做什么?
我必须准确地下载“骨架”架构。
谢谢你的回答。
我的情况是这样的,我想从数据库中删除一条用户记录。这很简单。但有两种可能的结果。
我的想法是,针对实体采取的行动Users应该在,UsersRepository所以这就是我的方法所在deleteUser($user)。是通过 ParamConverter 自动查询的$user用户对象并传递给存储库方法。
因为工作是在存储库中完成的,所以对我来说提供反馈是有意义的。
我如何addFlash()从我的App\Repository\UsersRepository extends ServiceEntityRepository?或者我应该在其他地方做这个“工作”?
我有一个 symfony 4 Web 应用程序,我正在尝试缩小 CSS,但它不起作用。我已经尝试了几个小时,但一直无法让它提供缩小版本。难道我做错了什么?
webpack.config.js
var Encore = require('@symfony/webpack-encore');
Encore
// the project directory where compiled assets will be stored
.setOutputPath('public/build/')
// the public path used by the web server to access the previous
directory
.setPublicPath('/build')
.cleanupOutputBeforeBuild()
.enableSourceMaps(!Encore.isProduction())
.splitEntryChunks()
.enableSingleRuntimeChunk()
.enablePostCssLoader()
// uncomment to create hashed filenames (e.g. app.abc123.css)
// .enableVersioning(Encore.isProduction())
// uncomment to define the assets of the project
.addEntry('js/app', './assets/js/app.js')
// .addStyleEntry('css/app', './assets/css/app.scss')
// uncomment if you use Sass/SCSS files
// .enableSassLoader()
// uncomment for legacy …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Docker 映像在 Symfony 上运行 Mercure。
symfony/mercure 版本:“v0.2.0”,
docker-compose.yaml 配置:
mercure:
container_name: mercure
image: dunglas/mercure
environment:
- JWT_KEY=MySecret
- DEMO=1
- ALLOW_ANONYMOUS=1
- PUBLISH_ALLOWED_ORIGINS=http://my_project.com:9090/hub
- DEBUG=1
- CORS_ALLOWED_ORIGINS=*
ports:
- "9090:80"
Run Code Online (Sandbox Code Playgroud)
当我去http://my_project.com:9090/可以看到可行的 Mercure 调试工具页面。Mercure 已正确安装并与 docker 容器一起运行。
Symfony .env 文件:
MERCURE_PUBLISH_URL=http://my_project.com/hub
MERCURE_JWT_SECRET=valid_generated_JWT_token
Run Code Online (Sandbox Code Playgroud)
MERCURE_JWT_SECRET 是使用来自 Docker 配置 (MySecret) 的 JWT_KEY 生成的有效 JWT 令牌和有效负载:
{
"mercure": {
"publish": []
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这样一个简单的例子时:
public function getCountUnreadMessagesAction(Publisher $publisher)
{
// some API logic
$update = new Update(
'http://my_project.com/api/v1/messages/count-unread',
json_encode(['count' => $count])
); …Run Code Online (Sandbox Code Playgroud) 当我对消息使用新的 ICU 格式时,多元化在 Symfony4.3 中部分工作。
预期行为: One item found
当前行为: {1, plural, =0 {No items found.} one {One item found.} other {# items found.} }
翻译文件适用于其他翻译,所以我认为配置是正确的。在translations/messagges.en.yaml
msg:
photos:
uploaded: >
{photos, plural,
=0 {No items found.}
one {One item found.}
other {# items found.}
}
Run Code Online (Sandbox Code Playgroud)
在模板中有以下行:
{{ 'msg.photos.uploaded'|trans({'photos': 1}) }}
Run Code Online (Sandbox Code Playgroud) 我试图在提交表单后重定向到“/”路由(同一页面):
/**
* @Route("/")
*/
Run Code Online (Sandbox Code Playgroud)
我的做法:
return $this->redirectToRoute('/');
Run Code Online (Sandbox Code Playgroud)
给了我以下错误:
无法为命名路由“/”生成 URL,因为此类路由不存在。
亲爱的智能蜂巢,
我在 symfony 4 中遇到了重定向问题。当然我试图找到一个类似的问题,但没有找到解决方案。
我刚刚通过一些教程开始学习 Symfony 4。(我喜欢它!!!)
该程序是本地 apache 服务器上的一个非常简单的博客示例(也许这很重要)。表示列表、添加、显示
所以我的问题出在 add 方法中。添加文章(工作正常)后,我想使用redirectToRout('article_list')。但这并没有把我带到任何地方,除了在添加页面上。
当我调试我的路线时,我可以看到正确的路线:
article_list ANY ANY ANY /article
article_delete DELETE ANY ANY /article/delete/{id}
article_add ANY ANY ANY /article/add
article_show ANY ANY ANY /article/{id}
Run Code Online (Sandbox Code Playgroud)
当我手动输入它们时,这些路线都可以正常工作。
add-function 也很简单:
/**
* @Route(
* "/article/add",
* name="article_add",
* )
*/
public function add(Request $request)
{
$article = new Article();
$form = $this->createFormBuilder($article)
[...]
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$article = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($article);
$entityManager->flush();
$this->redirectToRoute('article_list');
}
return $this->render('article/new.html.twig', array(
'form' …Run Code Online (Sandbox Code Playgroud) 我是 Symfony 的新手,我想使用choiceType.
所以我将这段代码添加到buildForm方法中:
$builder->add('applications', ChoiceType::class, [
'choices' => [
'-- Choisir --' => NULL,
'Admin' => '2',
'Super' => '1',
'visiteur' => '10',
'admin local' => '98',
'partenaire gestionnaire' => '16',
'admin national' => '15',
'soutien' => '11',
'exploitant' => '12',
'moe' => '99',
]
]);
Run Code Online (Sandbox Code Playgroud)
但是当我检查 HTML 代码时,我没有正确的数字:
<select id="user_applications" name="user[applications]" class="form-control">
<option value="0" selected="selected">-- Choisir --</option>
<option value="1">Admin</option>
<option value="2">Super</option>
<option value="3">visiteur</option>
<option value="4">admin local</option>
<option value="5">partenaire gestionnaire</option>
<option value="6">admin national</option>
<option value="7">soutien</option> …Run Code Online (Sandbox Code Playgroud) 我有一个带有授权服务的Symfony 4应用程序,我想处理一些更高级别的逻辑,以确定给定用户是否可以访问给定路由.
我实现了这个 BaseController
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Services\Authorize;
class BaseController extends Controller {
private $auth;
public function __construct(Authorize $auth){
$this->auth = $auth;
}
public function initBaseController() {
$this->auth->do_authorize();
}
}
Run Code Online (Sandbox Code Playgroud)
而Authorize服务是这样的:
namespace App\Services;
use Symfony\Component\HttpFoundation\RedirectResponse;
use App\Services\Session;
class Authorize {
private $session;
public function __construct(Session $session){
$this->session = $session;
}
public function do_authorize(){
if($this->session->validate('fake_token'){
return $this->redirectToRoute('login-portal'); // ** ERROR **
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,BaseController如果会话不存在或使用Authorize服务无效,则使用和重定向到登录页面全局实现此过程.
但是重定向行会抛出错误,尽管这是关于此主题的symfony文档中的正确实现.
我相信这是因为我试图从服务中重定向,而不是控制器.
如何从服务中重定向?