为什么在我的PHP链接中插入字符? [英] Why are characters inserted in my PHP link?

查看:55
本文介绍了为什么在我的PHP链接中插入字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个模块,但是链接不正确。

I created a module, but the link is not correct.

我的网站现在显示:

 /store/2?0=/cgv

正确的链接应为:

 /store/2/cgv

为什么不起作用?错误在哪里?

我应该在下面的代码中进行哪些更改才能获取链接?

Why doesn't it work ? where is the error ?
What should I change in the code below, to get the link ?

<?php

namespace Drupal\commerce_agree_cgv\Plugin\Commerce\CheckoutPane;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;

/**
 * Provides the completion message pane.
 *
 * @CommerceCheckoutPane(
 *   id = "agree_cgv",
 *   label = @Translation("Agree CGV"),
 *   default_step = "review",
 * )
 */
class AgreeCGV extends CheckoutPaneBase implements CheckoutPaneInterface {

  /**
   * {@inheritdoc}
   */
  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $store_id = $this->order->getStoreId();
    $pane_form['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $attributes = [
      'attributes' => [
        'class' => 'use-ajax',
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 800,
        ]),
      ],
    ];
    $link = Link::createFromRoute(
      $this->t('the general terms and conditions of business'),
      'entity.commerce_store.canonical',
      ['commerce_store' => $store_id, '/cgv'],
      $attributes
    )->toString();
    $pane_form['cgv'] = [
      '#type' => 'checkbox',
      '#default_value' => FALSE,
      '#title' => $this->t('I have read and accept @cgv.', ['@cgv' => $link]),
      '#required' => TRUE,
      '#weight' => $this->getWeight(),
    ];
    return $pane_form;
  }

}


推荐答案

因为 $ link 的构建不正确:

$link = Link::createFromRoute(
  $this->t('the general terms and conditions of business'), 
  'entity.commerce_store.canonical', 
  ['commerce_store' => $store_id, '/cgv'], # -> this is wrong
  $attributes
)->toString();




$ route_parameters :(可选)参数名称$ b的关联数组$ b和值。

$route_parameters: (optional) An associative array of parameter names and values.

您没有为第二个路由参数指定任何名称,因此相应的数组键回退到第一个可用的数字索引,即 0 ,表示 ['/ cgv'] 变为 [0 = > ‘/ cgv’] ,您将无法获得预期的链接。

You did not specify any name for the 2nd route parameters, so the corresponding array key fallback to the first available numeric indice, that is 0, meaning [ '/cgv' ] becomes [ 0 => '/cgv' ] and you don't get the link you expected.

我认为(如果我正确理解了您的问题),首先需要定义的是给定commerce_store的处理cgv的特定路线,即使用<附加code> / cgv :

I think (if I understood your issue correctly) what you need is to define in the first place that specific route handling cgv's for a given commerce_store, that is with the /cgv appended :

$route_collection = new RouteCollection();
$route = (new Route('/commerce_store/{commerce_store}/cgv'))
  ->addDefaults([
    '_controller' => $_controller,
    '_title_callback' => $_title_callback,
  ])
  ->setRequirement('commerce_store', '\d+')
  ->setRequirement('_entity_access', 'commerce_store.view');
$route_collection->add('entity.commerce_store.canonical.cgv', $route);

...以便您可以基于该特定路线建立链接:

... so that you can build links based on that specific route :

$link = Link::createFromRoute(
  $this->t('the general terms and conditions of business'), 
  'entity.commerce_store.canonical.cgv',
  ['commerce_store' => $store_id],
  $attributes
)->toString();

这篇关于为什么在我的PHP链接中插入字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆