承接 eltharin/ajaxresponser 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eltharin/ajaxresponser

最新稳定版本:V1.6.1

Composer 安装命令:

composer require eltharin/ajaxresponser

包简介

AjaxResponser Bundle for symfony

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

  • Require the bundle with composer:
composer require eltharin/ajaxresponser

What is AjaxResponser Bundle?

AjaxResponser is a bundle whitch allow to have routes responding with HTML or Json depending the request.

eg: a delete request in HTML will respond HTML page (or redirect) with a flash message, but the same page call by ajax, will respond a Json with the result and the flash message.

Use It :

Just Add AjaxOrNot Attribute to your route :

use Eltharin\AjaxResponserBundle\Annotations\AjaxCallOrNot;

...

#[Route(path: '/test-ajax-responser', name: 'app_test_ajax_responser')]
#[AjaxCallOrNot]
public function test_ajax_responser(): Response
{
    //-- Actions

    $this->addFlash('success', 'all is good');

    return $this->redirectToRoute('app_home');
}

If you go to /test-ajax-responser you will be redirect to your homepage with a flash message all is good

But if your call the same page in AJAX, with a header X-Requested-With with value XMLHttpRequest,

you got a Json Response :

{
    "statusCode": 302,
    "content": "<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=\"UTF-8\">\n        <title>Hello HomeController!<\/title>\n        <link rel=\"icon\" href=\"data:image\/svg+xml,<svg xmlns=%22http:\/\/www.w3.org\/2000\/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>\u26ab\ufe0f<\/text><\/svg>\">\n                \n                    <\/head>\n    <body>\n    <fieldset class=\"messageContainer\">\n        <legend>Flash Messages<\/legend>\n\n            <\/fieldset>\n        <style>\n    .example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px\/1.5 sans-serif; }\n    .example-wrapper code { background: #F5F5F5; padding: 2px 6px; }\n<\/style>\n\n<div class=\"example-wrapper\">\n    <h1>Hello HomeController! \u2705<\/h1>\n\n    This friendly message is coming from:\n    <ul>\n        <li>Your controller at <code><a href=\"file:\/\/D:\/laragon\/www\/testbundles\/src\/Controller\/HomeController.php#L0\">src\/Controller\/HomeController.php<\/a><\/code><\/li>\n        <li>Your template at <code><a href=\"file:\/\/D:\/laragon\/www\/testbundles\/templates\/home\/index.html.twig#L0\">templates\/home\/index.html.twig<\/a><\/code><\/li>\n    <\/ul>\n<\/div>\n\n    <script>\n        window.addEventListener(\"DOMContentLoaded\", (event) => {\n            Flashes.init('.messageContainer');\n        });\n    <\/script>\n    <\/body>\n<\/html>\n",
    "msgs": {
        "success": [
            "all is good"
       ]
    },
    "redirectUrl": "\/home"
}

you get :

  • statusCode : the original HTTP return code here 302 for the redirect
  • content : content returned after the redirect
  • msgs : flashes messages
  • redirectUrl : Url who generated the content

Get the content after redirect can offer you to don't make an other ajax for actualize actual page,

eg: you have a page showing many items, you want to delete one with a delete button, the route make the delete action and redirect you to the item page, so you have the HTML and can replace directly.

You can disable getting this content by set the getRedirectContent param to false :

use Eltharin\AjaxResponserBundle\Annotations\AjaxCallOrNot;

...

#[Route(path: '/test-ajax-responser', name: 'app_test_ajax_responser')]
#[AjaxCallOrNot(getRedirectContent: false)]
public function test_ajax_responser(): Response
{
    //-- Actions

    $this->addFlash('success', 'all is good');

    return $this->redirectToRoute('app_home');
}

now you have the redirect HTML.

统计信息

  • 总下载量: 70
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2024-02-18