SegmentFault symfony最新的文章
2018-01-18T12:10:19+08:00
https://segmentfault.com/feeds/blogs
https://creativecommons.org/licenses/by-nc-nd/4.0/
Symfony笔记之--自定义异常提示
https://segmentfault.com/a/1190000012888299
2018-01-18T12:10:19+08:00
2018-01-18T12:10:19+08:00
lcp0578
https://segmentfault.com/u/lcp0578
0
<h2>Custom Exception</h2>
<ul><li>ExceptionController</li></ul>
<pre><code> namespace KitBaseBundle\Controller;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use KitAdminBundle\Service\ThemeService;
class ExceptionController extends BaseController
{
private $theme;
public function __construct(\Twig_Environment $twig, $debug,ThemeService $theme)
{
parent::__construct($twig, $debug);
$this->theme = $theme;
}
/**
* @param Request $request
* @param string $format
* @param int $code An HTTP response status code
* @param bool $showException
*
* @return string
*/
protected function findTemplate(Request $request, $format, $code, $showException)
{
$themeName = $this->theme->get();
$name = $showException ? 'exception' : 'error';
if ($showException && 'html' == $format) {
$name = 'exception_full';
}
// For themes error pages
if (!$showException) {
$template = sprintf('@KitWeb/theme/'.$themeName.'/Exception/%s%s.%s.twig', $name, $code, $format);
if ($this->templateExists($template)) {
return $template;
}
}
// try to find a template for the given format
$template = sprintf('@KitWeb/theme/'.$themeName.'/Exception/%s.%s.twig', $name, $format);
if ($this->templateExists($template)) {
return $template;
}
return parent::findTemplate($request, $format, $code, $showException);
}
}</code></pre>
<ul><li>ThemeService.php</li></ul>
<pre><code><?php
namespace KitAdminBundle\Service;
class ThemeService
{
/**
*
* @var \Doctrine\Bundle\DoctrineBundle\Registry
*/
private $doctrine;
public function __construct($doctrine)
{
$this->doctrine = $doctrine;
}
public function get($default = 'Default')
{
/**
*
* @var \KitAdminBundle\Repository\ThemeRepository $repo
*/
$repo = $this->doctrine->getRepository('KitAdminBundle:Theme');
$theme = $repo->findOneBy([
'status' => 1
]);
return (!empty($theme) && !empty($theme->getPath())) ? $theme->getPath() : $default;
}
} </code></pre>
<ul>
<li>
<p>config.yml</p>
<pre><code># Twig Configuration
twig:
....
exception_controller: twig.controller.exception:showAction</code></pre>
</li>
<li>
<p>service.yml</p>
<pre><code>kit_admin.theme_service:
class: KitAdminBundle\Service\ThemeService
arguments: ["@doctrine"]
twig.controller.exception:
class: KitBaseBundle\Controller\ExceptionController
arguments: ['@twig','%kernel.debug%', '@kit_admin.theme_service']</code></pre>
</li>
<li>
<p>views</p>
<pre><code># KitWebBundle/Resources/views/theme/Luxe/Exception/error.html.twig
<h2>{{ theme_name }} self excption error</h2>
</code></pre>
</li>
</ul>
Symfony笔记之--控制器的快捷方法
https://segmentfault.com/a/1190000012888263
2018-01-18T12:08:38+08:00
2018-01-18T12:08:38+08:00
lcp0578
https://segmentfault.com/u/lcp0578
0
<h2>Shortcuts methods in Controller</h2>
<h4>1. 获取服务</h4>
<pre><code> $this->get($serviceId);</code></pre>
<h4>2. 重定向</h4>
<pre><code>
$this->redirectToRoute($routeName, $parameters, $status = 302);</code></pre>
<h4>3. 生成路由的url</h4>
<pre><code> $this->generateUrl($routeName, $parameters, $referenceType);</code></pre>
<h4>4. 返回一个json response</h4>
<pre><code>
$this->json($data, $status = 200, $headers = array(), $context = array());</code></pre>
<h4>5. 添加一个flash message</h4>
<pre><code> $this->addFlash($type, $message);</code></pre>
<h4>6. 判断是否授权进入某个方法</h4>
<pre><code> $this->isGranted('ROLE_ADMIN');</code></pre>
<h4>7. 判断用户是否授权,否,抛出异常</h4>
<pre><code>
$this->denyAccessUnlessGranted('ROLE_EDIT', $item, 'You cannot edit this item.');</code></pre>
<h4>8. 手动判断 CSRF token是否合法</h4>
<pre><code> $this->isCsrfTokenValid('token_id', $token);</code></pre>
<h4>9. 把请求转发到其他控制器和方法</h4>
<pre><code> $this->forward('GregwarCaptchaBundle:Captcha:generateCaptcha', ['key' => $key]);
//forward($controller, array $path = array(), array $query = array())
PS: 第二个参数为route上占位符参数,第三个为其他额外的参数</code></pre>
<h4>10. 文件下载(symfony >= 3.2)</h4>
<pre><code> $this->file();
//简单用法示例
return $this->file($docPath, $saveName);
// 如果是pdf之类,直接显示而不是下载,需要设置第三个参数ResponseHeaderBag::DISPOSITION_INLINE
// 还可以直接接受一个File或者UploadedFile实例
//$samplePdf = new File('/sample.pdf');
//return $this->file($samplePdf);</code></pre>
<h4>11. 读取配置参数</h4>
<pre><code> $this->getParameter('kernel.root_dir');</code></pre>
symfony笔记之--基础常用知识点
https://segmentfault.com/a/1190000012882755
2018-01-18T00:17:35+08:00
2018-01-18T00:17:35+08:00
lcp0578
https://segmentfault.com/u/lcp0578
0
<h2>Symfony Basic</h2>
<ul>
<li>
<p>获取登录用户信息</p>
<ul>
<li>
<p>控制器中</p>
<pre><code> $this->getUser();
//等价于
$this->get('security.token_storage')
->getToken()
->getUser(); </code></pre>
</li>
<li>
<p>Twig中:</p>
<pre><code> {% if app.user %}{{ app.user.username }}{% else %}游客{% endif %}</code></pre>
</li>
</ul>
</li>
<li>
<p>用户密码加密</p>
<pre><code> /**
*@var \Symfony\Component\Security\Core\Encoder\UserPasswordEncoder
*/
$encoder = $this->get('security.password_encoder');
</code></pre>
<p>校验密码:</p>
<pre><code> $encoder->isPasswordValid($user, $userForm->getPassword());</code></pre>
<p>密码加密:</p>
<pre><code> $password = $encoder->encodePassword($user, $userForm->getPlainPassword());
</code></pre>
</li>
<li>
<p>文件上传相关</p>
<pre><code> $file = $request->files->get('file'); //获取上传的文件
if($file instanceof UploadedFile){
//不为空
$filename = $this->get('kit.file_uploader')->upload($file, 'file');
}else{
//未上传
}
</code></pre>
</li>
<li>
<p>获取根目录和host</p>
<pre><code> $this->getContainer()->get('kernel')->getRootDir()
$root = $this->container->get('kernel')->getRootDir();
$root = $this->get('kernel')->getRootDir();
// xx/xxx/app
//Symfony 3.3
$root = $this->get('kernel')->getProjectDir()
%kernel.project_dir%
use %kernel.project_dir%/web/ instead of %kernel.root_dir%/../web/.</code></pre>
<p>get HOST</p>
<pre><code>
$request()->getHost()</code></pre>
</li>
<li>
<p>获取当前路由名称</p>
<pre><code> // controller
$routeName = $request->get('_route');
//twig
app.request.get('_route')</code></pre>
</li>
</ul>