CakePHP 3.8 - How to access public path to webroot in view? - cakephp

Migrating from 2.3.x to 3.8 and I can't figure out how I may access the public path to webroot in my views. Previously I could do something like this:
<link rel="stylesheet" type="text/css" href="<?php echo $this->webroot ?>wp-dist/acd76cde.css" />
But when I try this on 3.8 it says:
Error: webrootHelper could not be found.
I tried html helper, but can't find a helper method which gives just the public path to webroot. For example image() is relative to webroot/img, css() is relative to webroot/css and so on.
Am I missing something?

You're on the right track, you should be using the HTML helper.
For CSS:
<?= $this->Html->css('wp-dist/acd76cde.css'); ?>
Just FYI, internally this is using the options defined in config/app.php:
$pathPrefix = Configure::read('App.cssBaseUrl');
So in theory if you needed the same paths manually, you could read them out of the configs too - but you should just stick with the HTML Helper this is exactly what it's for.
From the Docs:
https://book.cakephp.org/3/en/views/helpers/html.html#linking-to-css-files
https://book.cakephp.org/3/en/views/helpers/html.html#inserting-well-formatted-elements
https://book.cakephp.org/3/en/development/configuration.html#general-configuration

Update:
As #ndm said in the comments above, the following works for me as well.
echo $this->Html->css('lightweight_lpbbd374e3', ['pathPrefix' => 'wp-dist/']);
Original my solution, which works too
$this->Url->webroot finally worked for me. So I'm including my css as follows:
<link rel="stylesheet" type="text/css" href="<?php echo $this->Url->webroot('wp-dist/lightweight_lpbbd374e3.css'); ?>" />
Until someone else suggests a better way, I'll continue using this.

Related

How can I change path of css and others in default.ctp in cake php?

My cake php code in localhost/admin/app/webroot
And in default.ctp file
The code is like this
echo $this->Html->css('style');
For this it is load
< link rel="stylesheet" type="text/css" href="/app/webroot/css/style.css" />
so its not loaded.
How can I gave full path here ?
Should I have to change .htaccess file for the same ?
Thanks in advance.
You can put full path in first argument
I found the answer :
If we can add
Configure::write('App.base', '/admin/');
in bootstrap.php file then it works for me.
Thanks for your help.

How to get absolute path for css and script

How to get absolute path for stylesheet or javascript files.
<?php echo $this->Html->css('/css/main.css');?>
Is giving URL
<link rel="stylesheet" type="text/css" href="/dev/theme/Default/css/main.css" />
And I am looking for
<link rel="stylesheet" type="text/css" href="http://localhost/dev/theme/Default/css/main.css" />
I'm really wondering why you would want to do so if the CSS files are served on the same domain as your website, but this will probably work;
<?php echo $this->Html->css($this->Html->url('/css/main.css', true));?>
HtmlHelper::url() (inherited from Helper::url() takes two arguments. The first is the path as a string or array (to make use of cakephp routing), if a Boolean 'true' is passed as its second argument, a 'full' URL is generated.
I'm not at my computer at the moment, so haven't been able to test it, but I think it should work.
update
I had a quick look at the source: https://github.com/cakephp/cakephp/blob/master/lib/Cake/View/Helper/HtmlHelper.php#L431
and HtmlHelper::css() uses Helper::assetUrl() internally;
https://github.com/cakephp/cakephp/blob/master/lib/Cake/View/Helper.php#L305
So this may work as well and if it works, it's a cleaner solution:
<?php echo $this->Html->css('main', null, array('fullBase' => true));?>

ie7 has dedicated css but it also loads the non dedicated one

hope sombody can enlighten me here,
i am writing a joomla template.
ie7 has a seperate css using:
<!--[if IE 7]>
<link rel="stylesheet" href="<?php echo $this->baseurl ;?>/templates/<?php echo $this->template ;?>/css/template_ie7.css" type="text/css" />
<![endif]-->
but is also loads the non ie7 css and accepts its styles, so nothing get fixed....
does anybody know what to do?
thanks,
Jonatahn
Using conditional css loading this way is not (at least for me) the best way to handle this situation.
As you are writing a template you can acces to the $_SERVER['HTTP_USER_AGENT'] variable in order to determine if the iExplorer (or any other) css is needed and then use php to filter it's load
<?php
if(stripos ( $_SERVER['HTTP_USER_AGENT'], "MSIE 7.0") > 0 ){
/* LOAD IE7 CSS here */
}else{
/* LOAD normal CSS here */
}
?>
This way you can load the IE7 css ONLY.

CakePHP linking css files and javascript files

How do you link css files and javascript/jquery files to a controller/view??
I am using CakePHP_1.3 and have located the following code online, but I cant seem to figure out where to put this, and secondly, where to place the css file named css_file.
<?php $this->Html->css("css_file", null, array("inline"=>false)); ?>
Any help appreciated guys...
You would put the CSS file in:
app/
webroot/
css/ <-- here
You would put the line of code in your default.ctp layout. This is located in (1.3):
cake/
libs/
view/
layouts/
default.ctp <-- copy this to app/views/layouts/
Then open that up app/views/layouts/default.ctp and look at the HTML. You'll see where Cake is already using this command. Simply replace the filename with the file you added. Do not add the .css to the end when you add the line of code.
you use echo $this->Html->css('css_file_name_without_the_extension'); and echo $this->Html->script('same') in the view
Based on your question, it sounds like you were asking how to do this on a view-by-view basis.
This is where $scripts_for_layout comes in useful.
You just need to make sure that it's in your <head> tag in /app/views/layouts/default.ctp as <?php echo $scripts_for_layout; ?>
Then, you can literally add the code you included in your question into any of your views, at literally any point you like. Because you have 'inline' => false it won't actually appear at that position in the view:
<?php $this->Html->css("css_file", null, array("inline"=>false)); ?>
...and you'll find that css_file.css is automatically linked in your <head> whenever that particular view is loaded. This is a great way to only load specific CSS files on a per-view basis when they're needed, yet make sure they appear in the <head> tag where they should be.
This also works for JavaScript as follows:
<?php $this->Html->script("js_file", null, array("inline"=>false)); ?>
If you want to have one or more CSS files loaded in all views, just put a shortened version your code in the <head> of default.ctp as follows:
<?php echo $html->css(array('each','one','of','your','css','files'));
Or, again, for JavaScript:
<?php echo $html->script(array('each','one','of','your','js','files'));
Don't include the .css or .js extension in the array.

href links in cakephp

In CakePHP,if i give href links as href="/css/main.css" it is not refering to the css folder in the webroot. Only when I mention href="http://localhost/cake/app/webroot/css/main.css" the css gets applied.
<link type="text/css" rel="Stylesheet" href="/css/main.css" media="screen,projection" />
This does not apply the specied css.
What is the reason for this?
Why is the code not identifying the correct folder?
Because it starts with a /, it is treated as an absolute path (from the root of the site). The browser translates it to
http://localhost/css/main.css
You can either specify the correct absolute path
/cake/app/webroot/css/main.css
or the full path
http://localhost/cake/app/webroot/css/main.css
or a relative path, for example
../css/main.css
echo $html->css('main');
BOOK
API
Why are you not using core helper? It will generate required path to CSS file Inserting Well-Formatted elements
And check main configuration file(/app/config/core.php), maybe you are not using mod_rewrite. Check core.php for commented this line Configure::write('App.baseUrl', env('SCRIPT_NAME'));
If your absolute URL looks like
http://localhost/cake/app/webroot/css/main.css
and your URL is
/css/main.css
I suppose the browser translates the URL you gave to
http://localhost/css/main.css
(you can check that with Firebug, "net" tab for instance)
If that's the case, you should :
use a relative URL with no leading /
use an absolute URL that refers to the root URL
But, in my opinion, using the URL that starts with 'http://' is the best way to be sure that your CSS inclusion is always OK, no matter which directory or URL you are into...
A function like this to define a BASEURL allows you to only update in one place.. I use something like...
define("DEVELOPMENT", true);
function setReporting()
{
if (DEVELOPMENT)
{
define("BASEURL", "http://localhost/localDir", true);
error_reporting(E_ALL);
}
else
{
define("BASEURL", 'http://' . $_SERVER['SERVER_NAME'], true);
error_reporting(0);
}
}
setReporting();
When you deploy cake, the url should just be /css/main.css as the server's DocumentRoot will point to the cake/app/webroot directory.
I suggest reading this article from Cake's online docs for more info. Note that this article refers to the 1.1 version of Cake, but should work in 1.2 as well.
Good luck!

Resources