0

Who can tell me how to change the link from PHP.

$this->pageConfig->addPageAsset('')

I already tried but not working.

$this->getUrl('https://...')

Thank you!

4
  • you want to redirect on another page? Commented Sep 24, 2021 at 4:36
  • Can you specify what you are try to access Commented Sep 27, 2021 at 6:12
  • i want to add my cdn css link in this php file, not same domain. @GohilRajesh
    – niceonelee
    Commented Sep 28, 2021 at 7:45
  • Yes, is my cdn link. @DhirenVasoya
    – niceonelee
    Commented Sep 28, 2021 at 7:46

3 Answers 3

0

You can use this code to add css file on your phtml file.

<link rel="stylesheet" type="text/css" href="YOURCSSURL" />
0

I would try to add this method on your block template :

protected function _prepareLayout()

And then customise your metadata as you want in there. But this is the same thing as updating directly the phtml file as mentionned by @Dhiren Vasoya

0

To include an external CSS file from xml, add

https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/css-topics/css-themes.html

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <css src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"  src_type="url" rel="stylesheet" type="text/css"  />
    </head>
</page>

To include an external CSS file from php

Step 1 – Create a block class in your module

use Magento\Framework\View\Element\Template;
 
class Head extends Template
{
    /**
     * @var \Magento\Framework\View\Asset\Repository
     */
    protected $assetRepository;
 
    /**
     * Header constructor.
     * @param Template\Context $context
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->assetRepository = $context->getAssetRepository();
    }
 
    /**
     * @return string
     */
    public function getUrlCSS()
    {
        $asset_repository = $this->assetRepository;
        $asset  = $asset_repository->createAsset('URL to External Source');
        $url    = $asset->getUrl();
         
        return $url;
    }
}

Step 2 – Create corresponding phtml for the class created in Step 1

$url = $block->getUrlCSS();
if ($url):
    echo '<link rel="stylesheet" type="text/css" media="all" href="'.$block->getUrlCSS().'" />'
endif;

Step 3 – Create layout xml file to call the above phtml file in the head section of your website under

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.