欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

模板 参数 blade url Laravel getQueryString() with out & amp 有大用

Laravel getQueryString() with out &

I'm trying to output a variable containing part of a URL into Javascript on my page. When I do this, it changes any & into &, which breaks the URL.

I've tried using str_replacehtml_entity_decodehtmlspecialchars_decode but the result was always same & instead of &.

How i can get it with & instead of & ?

This is my controller:

//here we catch the query
public function index(Request $request)
    {
        $input   = $request->getQueryString();

        return view('lots.browse')->withInput($input);
    }

// here we filter the ajax request
public function indexData(LotFilters $filters)
    {
       $lots  = Lot::filter($filters);

        return Datatables::eloquent($lots)->make(true);
    }

This is my view:

<!-- Datatables -->
    <script type="text/javascript">
        $(document).ready(function(){
            $('#table').DataTable({
                searching: false,
                processing: true,
                serverSide: true,
                ajax: '{{  url("/data") . "?" .  html_entity_decode($input) }}',
                columns: [
                    { data: 'date', name: 'date' },
                    { data: 'bid', name: 'bid' },
                    { data: 'auction_name', name: 'auction_name' },
                    { data: 'pics_urls', name: 'pics_urls' },
                    { data: 'company', name: 'company' },
                    { data: 'model_name_en', name: 'model_name_en' },
                    { data: 'model_type_en', name: 'model_type_en' },
                    { data: 'grade_en', name: 'grade_en' },
                    { data: 'mileage_en', name: 'mileage_en' },
                    { data: 'model_year_en', name: 'model_year_en' },
                    { data: 'color_en', name: 'color_en' },
                    { data: 'displacement', name: 'displacement' },
                    { data: 'transmission_en', name: 'transmission_en' },
                    { data: 'scores_en', name: 'scores_en' },
                    { data: 'start_price_en', name: 'start_price_en' },
                    { data: 'result_en', name: 'result_en' }
                ]
            });
        });
    </script>

And this is the result:

enter image description here

3 Answers 正确答案 

When using a blade template, {{ $variable }} will properly encode any characters that should not be placed directly in HTML. Since you're placing this variable in Javascript, you don't want this to happen.

The simplest solution is to disable HTML-encoding: {{!! $variable !!}}.

But this is not the full solution. You need to ensure that the variable isn't blindly output, since if it contains a single quote, it will break your Javascript.

The full solution is to escape the string for single quotes:

{{!! addcslashes($input, "'") !!}}

    try using rawurldecode it should fixe it.

      in stead of blade {{ $input }} just used <?php echo $input ?> and link renders with out changing & in to &amp; . So this is just problem of blade.php , i don't know why it is like that, but i finally managed to solve my problem.

      So this is my script:

      <script type="text/javascript">
              $(document).ready(function(){
                  $('#table').DataTable({
                      searching: false,
                      processing: true,
                      serverSide: true,
                      ajax: "<?php echo '/data?' .  $input ?>",
                      columns: [
                          { data: 'date', name: 'date'},
                          { data: 'bid', name: 'bid' },
                          { data: 'auction_name', name: 'auction_name' },
                          { data: 'pics_urls', name: 'pics_urls' },
                          { data: 'company', name: 'company' },
                          { data: 'model_name_en', name: 'model_name_en' },
                          { data: 'model_type_en', name: 'model_type_en' },
                          { data: 'grade_en', name: 'grade_en' },
                          { data: 'mileage_en', name: 'mileage_en' },
                          { data: 'model_year_en', name: 'model_year_en' },
                          { data: 'color_en', name: 'color_en' },
                          { data: 'displacement', name: 'displacement' },
                          { data: 'transmission_en', name: 'transmission_en' },
                          { data: 'scores_en', name: 'scores_en' },
                          { data: 'start_price_en', name: 'start_price_en' },
                          { data: 'result_en', name: 'result_en' }
                      ]
                  });
              });
          </script>
      

      And my controller:

      public function index(Request $request)
          {
              $input   = $request->getQueryString();
      
              return view('lots.browse')->withInput($input);
          }
      

      And the result :

      enter image description here

        来自  https://stackoverflow.com/questions/44402896/laravel-getquerystring-with-out-amp





        Laravel getQueryString() with out &amp;

        我想输出一个变量包含URL的一部分,在我的网页上的JavaScript。当我做这些的时候,它的任何变化&#38;进入之内&#38;,这打破了URL

        我试着用_ str replacehtml_entity_decodedecode htmlspecialchars _但结果总是相同的&#38;而不是&#38;

        我如何能得到它&#38;而不是&#38;

        这是我的控制器:

        //here we catch the querypublic function index(Request $request)    {        $input   = $request->getQueryString();        return view('lots.browse')->withInput($input);    }// here we filter the ajax requestpublic function indexData(LotFilters $filters)    {       $lots  = Lot::filter($filters);        return Datatables::eloquent($lots)->make(true);    }

        这是我的看法:

        <!-- Datatables -->    <script type="text/javascript">        $(document).ready(function(){            $('#table').DataTable({                searching: false,                processing: true,                serverSide: true,                ajax: '{{  url("/data") . "?" .  html_entity_decode($input) }}',                columns: [                    { data: 'date', name: 'date' },                    { data: 'bid', name: 'bid' },                    { data: 'auction_name', name: 'auction_name' },                    { data: 'pics_urls', name: 'pics_urls' },                    { data: 'company', name: 'company' },                    { data: 'model_name_en', name: 'model_name_en' },                    { data: 'model_type_en', name: 'model_type_en' },                    { data: 'grade_en', name: 'grade_en' },                    { data: 'mileage_en', name: 'mileage_en' },                    { data: 'model_year_en', name: 'model_year_en' },                    { data: 'color_en', name: 'color_en' },                    { data: 'displacement', name: 'displacement' },                    { data: 'transmission_en', name: 'transmission_en' },                    { data: 'scores_en', name: 'scores_en' },                    { data: 'start_price_en', name: 'start_price_en' },                    { data: 'result_en', name: 'result_en' }                ]            });        });    </script>

        这就是结果:

        enter image description here

        3个答案 

         正确答案 


        用刀模板时,{ { $变量} }将正确编码的任何字符不应直接放置在HTML。因为你把这个变量在JavaScript中,你不希望这样的事情。

        最简单的办法是禁用HTML编码:{ {!!$变量!} }!

        但这并不是全部的解决方案。你需要确保变量不是盲目的输出,因为如果它包含一个单引号,它会打破你的JavaScript。

        完整的解决方案是逃单引号的字符串:

        { {!($!是要编码的输入,“&#39;”)!} }!

          试着用rawurldecode应该给它。

            而叶片{ { $输入} }只是用<?php echo $input ?>链接呈现出变化&#38;&#38;。所以这只是问题的blade.php,我不知道为什么它是这样的,但我终于解决我的问题。

            所以这是我的剧本:

            <script type="text/javascript">        $(document).ready(function(){            $('#table').DataTable({                searching: false,                processing: true,                serverSide: true,                ajax: "<?php echo '/data?' .  $input ?>",                columns: [                    { data: 'date', name: 'date'},                    { data: 'bid', name: 'bid' },                    { data: 'auction_name', name: 'auction_name' },                    { data: 'pics_urls', name: 'pics_urls' },                    { data: 'company', name: 'company' },                    { data: 'model_name_en', name: 'model_name_en' },                    { data: 'model_type_en', name: 'model_type_en' },                    { data: 'grade_en', name: 'grade_en' },                    { data: 'mileage_en', name: 'mileage_en' },                    { data: 'model_year_en', name: 'model_year_en' },                    { data: 'color_en', name: 'color_en' },                    { data: 'displacement', name: 'displacement' },                    { data: 'transmission_en', name: 'transmission_en' },                    { data: 'scores_en', name: 'scores_en' },                    { data: 'start_price_en', name: 'start_price_en' },                    { data: 'result_en', name: 'result_en' }                ]            });        });    </script>

            我的控制器:

            public function index(Request $request)    {        $input   = $request->getQueryString();        return view('lots.browse')->withInput($input);    }

            结果:

            enter image description here

              来自  https://stackoverflow.com/questions/44402896/laravel-getquerystring-with-out-amp




              & a m p; replace or decode in Laravel 5.4

              PUBLISHED 11 MONTHS AGO BY DMITRY.MALYS

              it is a newbie question. But i spend 3 hours to solve it with 0 result.

              i want to convert this link:

              http://127.0.0.1:8000/data?make%5B%5D=ACURA&amp;model%5B%5D=LEGEND&amp;score%5B%5D=6

              in to this:

              http://127.0.0.1:8000/data?make%5B%5D=ACURA&model%5B%5D=LEGEND&score%5B%5D=6

              This is my controller:

              `public function index(Request $request)
                  {
                      $inputRAW    = $request->getQueryString();
              
                      $inputDECODE = htmlspecialchars_decode($inputRAW);
              
                      $input       = str_replace('&amp;', '&', $inputDECODE);
              
                      return view('lots.browse', compact('input'));
                  }`
              

              But it didn't work.

              Some on know what im doing wrong?

              Best Answer(As Selected By dmitry.malys)
              dmitry.malys

              i found the solution. In order to get rid of & a m p; you need to use < ? php ? > instead of blade {{ }}

              ActiveMonkey

              when I try this:

                      $inputRAW    = 'http://127.0.0.1:8000/data?make%5B%5D=ACURA&amp;model%5B%5D=LEGEND&amp;score%5B%5D=6';
              
                      $inputDECODE = htmlspecialchars_decode($inputRAW);
              
                      $input       = str_replace('&amp;', '&', $inputDECODE);
              
                      dd($input, 'http://127.0.0.1:8000/data?make%5B%5D=ACURA&model%5B%5D=LEGEND&score%5B%5D=6');
              
              

              I get exactly what you are looking for. What exactly does "getQueryString();" do?

              dmitry.malys

              Thank you fro replay, i have a search form witch will submit to page "lots", on this page a have a ajax witch will catch that and return filtered result:

              this is full controller: public function index(Request $request) { $inputRAW = $request->getQueryString();

                  $inputDECODE = htmlspecialchars_decode($inputRAW);
              
                  $input     = str_replace('&amp;', '&', $inputDECODE);
              
                  return view('lots.browse', compact('input'));
              }
              
              public function indexData(LotFilters $filters)
              {
                 $lots  = Lot::filter($filters);
              
              
                  return Datatables::eloquent($lots)->make(true);
              }
              

              This is my js:

                  $(document).ready(function(){
                      $('#table').DataTable({
                          bInfo: false,
                          searching: false,
                          processing: true,
                          serverSide: true,
                          ajax: '{{  url("/data") . "/?" . $input }}',
                          columns: [
                              { data: 'date', name: 'date' },
                              { data: 'bid', name: 'bid' },
                              { data: 'auction_name', name: 'auction_name' },
                              { data: 'pics_urls', name: 'pics_urls' },
                              { data: 'company', name: 'company' },
                              { data: 'model_name_en', name: 'model_name_en' },
                              { data: 'model_type_en', name: 'model_type_en' },
                              { data: 'grade_en', name: 'grade_en' },
                              { data: 'mileage_en', name: 'mileage_en' },
                              { data: 'model_year_en', name: 'model_year_en' },
                              { data: 'color_en', name: 'color_en' },
                              { data: 'displacement', name: 'displacement' },
                              { data: 'transmission_en', name: 'transmission_en' },
                              { data: 'scores_en', name: 'scores_en' },
                              { data: 'start_price_en', name: 'start_price_en' },
                              { data: 'result_en', name: 'result_en' }
                          ]
                      });
                  });
              

              Im sorry if expalin bad, please ask me to give more details if you need.

              dmitry.malys

              i found the solution. In order to get rid of & a m p; you need to use < ? php ? > instead of blade {{ }}

              Please sign in or create an account to participate in this conversation.


              来自  https://laracasts.com/discuss/channels/laravel/amp-replace-or-decode-in-laravel-54?page=0


              How to create link with blade templating

              PUBLISHED 2 YEARS AGO BY OSHERDO

              Hey I want to create a link to other page in my app. How do I do it with laravel's blade engine?

              Best Answer(As Selected By osherdo)
              Snapey

              @osherdo - no, you just need to know the name of the route, or the URL. So you do need to know how your page will be referenced and if you want to create and use a named route. Otherwise, it's just plain html.

              You can use plain html like <a href='/about'>About</a>

              or you can use helpers to named routes <a href='{{ route('about') }}'>About</a> (which I personally dislike)

              or you can use a URL helper <a href='{!! url('/about'); !!}'>About</a>

              These last two are URL helpers, and the only blade element is how to insert content with double braces or {!! for raw html.

              Snapey
               Snapey
              2 years ago(872,295 XP)

              Start with leaning about Routing. Its not an issue related to Blade since you just create a link with the name of the other page.

              osherdo

              I am actually trying to write a link (just like ahref) to another page in the app. I don't need routing for this - right?

              Snapey
               Snapey
              2 years ago(872,295 XP)

              @osherdo - no, you just need to know the name of the route, or the URL. So you do need to know how your page will be referenced and if you want to create and use a named route. Otherwise, it's just plain html.

              You can use plain html like <a href='/about'>About</a>

              or you can use helpers to named routes <a href='{{ route('about') }}'>About</a> (which I personally dislike)

              or you can use a URL helper <a href='{!! url('/about'); !!}'>About</a>

              These last two are URL helpers, and the only blade element is how to insert content with double braces or {!! for raw html.

              osherdo

              @Snapey thanks for that. I got it now.

              Please sign in or create an account to participate in this conversation.


              来自  https://laracasts.com/discuss/channels/general-discussion/how-to-create-link-with-blade-templating

              普通分类: