`
sillycat
  • 浏览: 2566963 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Symfony(4)Pages in Symfony2

    博客分类:
  • PHP
 
阅读更多

Symfony(4)Pages in Symfony2

There is a simple two-step process.
Create a Route
Create a Controller

1. Environment and Front Controllers
There are 3 environment defined — dev, test and prod.

Symfony2 comes with 2 web-accessible front controller: app_dev.php and app.php.

When running in debug mode, Symfony2 runs slower, but all changes are reflected without having to manually clear the cache.

2. Build Simple Hello Page
A bundle is nothing more than a directory that houses everything related to a specific feature, including PHP classes, configuration, and even stylesheets and JavaScript files.

Create the Bundle
>php app/console generate:bundle --namespace=Acme/HelloBundle --format=yml

After that a directory(bundle) is created /src/Acme/HelloBundle, and one line is added to the file app/AppKernel.php.
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new Acme\HelloBundle\AcmeHelloBundle(),
        );

Create Router
The root router is already there in #app/config/routing.xml
helloBundle is already there
acme_hello:
    resource: "@AcmeHelloBundle/Resources/config/routing.yml"
    prefix:   /

in the sub routing.xml file src/Acme/HelloBundle/Resources/config/routing.yml
acme_hello_homepage:
    pattern:  /hello/{name}
    defaults: { _controller: AcmeHelloBundle:Default:index }

Create the Controller
Based on the configuration, the src/Acme/HelloBundle/Controller/DefaultController.php will be called and the method should be indexAction
<?php
namespace Acme\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('AcmeHelloBundle:Default:index.html.twig', array('name' => $name));
    }
}

Then I visit the page http://sdk-consumer.local.sillycat.com/hello/good, it will give me the pages.

I am running that under apache. Here is another way to run it.
>php app/console cache:clear --env=prod --no-debug
>php app/console server:run --env=prod

Create The Template
Instead of writing the HTML inside the controller, render a template file instead.

return $this->render('AcmeHelloBundle:Default:index.html.twig', array('name' => $name));

BundleName:ControllerName:TemplateName
The physical location should be /path/to/BundleName/Resources/views/ControllerName/TemplateName

The twig file template will be as follow:
{# src/Acme/HelloBundle/Resources/views/Hello/index.html.twig #}
 {% extends '::base.html.twig' %}

 {% block body %}
     Hello {{ name }}!
 {% endblock %}

Pay attention to the ::base.html.twig, it is missing the BundleName:ControllerName, that means it is under the app/Resources

3. The Directory Structure
The Web Directory
Our front controller is there, app.php, app_dev.php. Actually our visit can be as follow:
http://sdk-consumer.local.sillycat.com/app_dev.php/hello/sillycat

Bundle Directory Structure
Controller/    — contains the controllers of the bundle
DependencyInjection/ — holds certain dependecy injection extension classes, for example, service configuration, compiler passes (optional)
Resources/config/ 
Resources/views/
Resources/public/ contains web assets(images, stylesheets, etc) and is copied or symbolically linked into the project web/ directory via the assets:install console command
Tests/

>php app/console assets:install


Tips
1. Permission Error when Execute composer update
Error Message:
  [ErrorException]                            touch(): Utime failed: Permission denied

Solution:
Find touch and change the rights
>which touch
/usr/bin/touch

>sudo chmod 755 /usr/bin/touch

Error Message:
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-update-cmd event terminated with an exception                                                                     [RuntimeException]                                                           An error occurred when executing the "'cache:clear --no-warmup'" command.

Solution:
Run this command first, before we do composer update
>sudo rm -fr app/cache/*
>sudo rm -fr app/logs/*
>sudo rm -fr web/bundles/*

The root reason is that the apache is running with user daemon, my current user is carl. So I need to fix as follow:
>sudo chmod +a "daemon allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs web/bundles

>sudo chmod +a "carl allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs web/bundles

References:
http://composer.golaravel.com/04-schema.html#extra   Configuration Doc for Composer
http://www.copernica.com/en/support/rest/example-get-post-put-and-delete-requests curl with PHP

http://kiwwito.com/unable-to-write-cache-file-problem-in-symfony-2/  Permission with composer and apache

分享到:
评论

相关推荐

    vue-symfony-starter:一个使用Vue.js和Symfony的“简单”入门项目

    请参阅上的GitHub Pages。 :chart_increasing: 变化 此项目的所有显着更改将记录在文件中。 这个CHANGELOG是用 :red_heart_selector: 通过 。 该项目遵循。 :bookmark: 路线图 见 :spouting_whale: 支持的...

    symfony-static-dumper:[只读]将您的Symfony应用程序转储到静态HTML网站

    对于部署到Github Pages和其他非PHP静态网站托管很有用。安装composer require symplify/symfony-static-dumper 如果您不使用Flex,请添加到config/bundles.php : return [ Symplify \ SymfonyStaticDumper \ ...

    PHP Reactive Programming

    Use the RxPHP library in combination with Symfony Console The different approaches to using Symfony3's Event Dispatcher component Test your reactive PHP code using PHPUnit Analyze ...

    AmpHtmlBundle-master.rar

    4. `config/`: 存放 Symfony 配置文件,如`services.yaml`,定义了服务及其依赖。 5. `tests/`: 测试代码目录,包括单元测试和集成测试,确保代码的正确性。 6. `composer.json`: 描述项目信息、依赖关系及自动加载...

    基于PHP的DLOG4J JSP v3.0 Beta2 开源个人版.zip

    这个版本是DLOG4J的第三大主要版本,并且处于Beta2测试阶段,意味着它可能包含一些未解决的问题,但已经具备了基本功能,适合开发人员试用和反馈。 【描述】"基于PHP的DLOG4J JSP v3.0 Beta2 开源个人版.zip" 暗示...

    PHP 7 Data Structures and Algorithms

    2017 | ASIN: B01IF7NLDW | 340 Pages | AZW3 | 2.55 MB Key Features Gain a complete understanding of data structures using a simple approach Analyze algorithms and learn when you should apply each ...

    pages

    2. **变量和数据类型**:PHP支持多种数据类型,包括字符串、整型、浮点型、数组、对象、布尔值和NULL。变量以$符号开头,无需提前声明类型。 3. **流程控制**:PHP提供条件语句(如if...else、switch)、循环(for...

    Asp转Php工具 asp2php

    4. 扩展性:PHP有大量的开源库和框架,如Laravel、Symfony等,而ASP虽然也有组件,但数量和多样性不及PHP。 在使用Asp2Php进行转换时,开发者应考虑以下几点: 1. 测试:转换后,必须对所有PHP文件进行全面测试,...

    网络硬盘(C#,php,asp源代码)

    4. PHP框架:如Laravel或Symfony,可以简化开发流程并提供更好的代码组织结构。 最后,我们讨论ASP(Active Server Pages)的实现。ASP是微软早期的服务器端脚本技术,虽然现在被ASP.NET取代,但在一些旧系统中仍然...

    管理系统界面资源前端

    4. **Pages**: "pages"目录可能包含了系统的各个视图或路由,每个文件可能代表一个独立的管理页面,如登录页、用户管理页、数据统计页等。页面结构通常由HTML、CSS和JavaScript共同构建,其中HTML负责内容结构,...

    OC-Projet-5_Blog

    OC-Projet-5_博客Projet博客Openclassrooms du parcoursdéveloppeurd'applications PHP / SYMFONY。... 可以在以下页面上进行以下网站的网站制作: les pages utilesàtous les访问者; les page permettant

    2022年PHP面试题.doc

    2. **ASP, PHP, JSP的优缺点比较**: - ASP(Active Server Pages)使用VBScript或JavaScript作为脚本语言,易于学习,但性能相对较低。 - PHP(Hypertext Preprocessor)跨平台,语法简洁,支持多种数据库,并且...

    Dash for Mac 2.0.2 文档查看神器 破解版

    iOS OS X .NET Framework Man Pages ActionScript Akka Android AngularJS Ansible Appcelerator Titanium Arduino Backbone Bash Boost Bootstrap Bourbon Bourbon Neat C C++ CakePHP Cappuccino Chai Chef ...

    Apache2,需要的可以看看

    Apache2.0在JAVA开发中的应用主要体现在其对Servlets和JSP(JavaServer Pages)的支持。通过集成Tomcat或Jetty等Servlet容器,Apache能够运行JAVA Web应用程序,提供动态内容服务。开发者可以利用这些技术构建企业级...

    计算机专业的毕业设计.docx

    2. JAVA类: - Java是一种跨平台的面向对象编程语言,常用于企业级应用开发。设计题目可能涵盖Java桌面应用、Web应用或移动应用。 - 知识点:Java核心技术(如集合、多线程、IO流、网络编程)、Java Swing或JavaFX...

    phpjsp教程

    4. **JSTL(JavaServer Pages Standard Tag Library)**:提供一系列标准标签,简化JSP页面的编写。 5. **Servlet**:虽然不是JSP的一部分,但作为JSP的基础,理解Servlet的工作原理和API非常重要。 6. **MVC(Model...

    计算机专业毕业设计参考题目.doc

    2. JAVA类:Java是一种广泛使用的面向对象的编程语言,其毕业设计可能涵盖: - Java SE和Java EE的区别与应用场景 - Servlet和JSP的使用,理解MVC模式 - Spring框架的使用,包括Spring Boot和Spring Cloud - ...

    计算机专业毕业设计参考题目 (3).docx

    2. JAVA:Java是一种广泛使用的面向对象的编程语言,适用于跨平台应用开发。Java题目可能涵盖: - 使用Servlet和JSP开发Web应用。 - Spring框架的应用,包括Spring Boot、Spring MVC、Spring Data等。 - ...

    计算机专业毕业设计方案参考题目.doc

    2. JAVA类题目: Java是一种广泛使用的面向对象的编程语言,适用于各种平台。Java毕业设计涉及的知识点包括: - Java基础语法和面向对象编程 - Spring框架的使用,包括Spring Boot和Spring MVC - 数据库连接池和...

    基于PHP的淘客JSP在线客服管理系统TaokeOCS v站点版.zip

    【标题】"基于PHP的淘客JSP在线客服管理系统TaokeOCS v站点版.zip" 涉及的核心技术是PHP编程语言与JSP(JavaServer Pages)在构建在线客服系统中的结合应用。淘客,是指通过推广电商平台的商品赚取佣金的网络营销...

Global site tag (gtag.js) - Google Analytics