base_url in CakePHP
Yes, there is. In your view, you may access:
<?php echo $this->webroot; ?>
Also, your host information is stored in the $_SERVER['HTTP_HOST']
variable in case you want that.
In your controller, if you want full URLs, use this:
Router::url('/', true);
How to get the base Url in cakephp?
The exact same command should work:
<?php
echo $this->Html->css('reset.css');
?>
It automatically adds the path to the CSS folder if the given path 'reset.css'
doesn't start with a slash.
By the way, if you do need to get the base url in Cake, you can use the Router
class:
//with http://site.domain.com/my_app
echo Router::url('/') //-> /my_app
echo Router::url('/', true) //-> http://site.domain.com/my_app
How to get base url inside controller in CakePHP
You want to use Router::url()
to get the full URL. For example to get the full web address for the homepage:-
Router::url('/', true);
You can pass any router array as the first parameter to get the full URL as long as you pass true
as the second parameter. For example:-
$url = Router::url(
['controller' => 'pages', 'action' => 'display', 'test'],
true
);
how to set base url in cake php 3 in config/app.php file and get that url in all view
You probably don't need to do that, as the Helpers
in Cake do that automatically.
For example, if your app is under http://localhost/somepath, creating a link like this
echo $this->Html->link('home', '/');
will automatically point to http://localhost/somepath
Links to actions work the same way:
echo $this->Html->link('login', ['controller' => 'Users', 'action' => 'login']);
will automatically point to http://localhost/somepath/Users/login
And if you do need to get the url anywhere else than in a view, you can do it like this:
use Cake\Routing\Router;
$path = Router::url('/', true);
How do you generate a link to the base URL in CakePHP 3?
You can use the Router Class
<a href="<?= \Cake\Routing\Router::url(['controller' => 'Front', 'action' => 'index']) ?>">
<img class="app-logo" src="/app-logo.png">
<span class="app-name">App name</span>
</a>
Edit: CakePHP 3.x uses Namespaces. You have to use the fully qualified class name or add a use statement to the top of your script.
echo \Cake\Routing\Router::url(...);
or
use Cake\Routing\Router;
echo Router::url(...);
Related Topics
How to Keep Form Values After Post
Partially Hide Email Address in PHP
How to Convert Utc Datetime to Another Timezone
How to Find First Day of the Next Month and Remaining Days Till This Date with PHP
Post Data and Retrieve the Response Using PHP Curl
How to Read Xml File from Url Using PHP
Updating Column So That It Contains the Row Position
Reading from Comma or Tab Delimited Text File
Concat Columns with Laravel 5 Eloquent
Correct Format for Strings/Numbers Beginning with Zero
Return One Value from Database with MySQL PHP Pdo
PHP - Cannot Use a Scalar as an Array Warning
How to Escape String from PHP for JavaScript
Line Break Not Working When Writing to Text File in PHP
Declaring a Global Variable Inside a Function