🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1、新建并激活Bundle【AppBundle】 ~~~ php app/console generate:bundle ~~~ `编辑:app/AppKernel.php` ~~~ <?php public function registerBundles() { return array( #…… new AppBundle\AppBundle(), ); } ~~~ 2、新建controller【AppBundle:Ajax】 ~~~ php app/console generate:controller ~~~ 3、新建Action `编辑:src/Appbundle/Controller/DefaultController` ~~~ <?php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; class DefaultController extends Controller{ /** * @Route("/test", name="test") * @Template() */ public function testAction(){ return array(); } } ~~~ `编辑:src/Appbundle/Controller/AjaxController` ~~~ <?php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Response; class AjaxController extends Controller{ /** * @Route("/ajax_test", name="ajax_test") */ public function ajax_testAction(){ return new Response('今天是'.$_REQUEST['d']); } } ~~~ 4、新建Twig模板 `新建:src/Appbundle/Resources/views/Default/test.html.twig` ~~~ <script type="text/javaxcript" src="jquery"></script> <script type="text/javaxcript"> $(function(){ $("p").click(function(){ var d = $(this).text(); $.ajax({ type: "get", url: "{{ path('ajax_text') }}", data: { d : d }, success: function(r){ alert(r); } }); }) }) </script> <h1>test ajax</h1> <p>星期一</p> <p>星期二</p> <p>星期三</p> <p>星期四</p> <p>星期五</p> <p>星期六</p> <p>星期日</p> ~~~