Base_Url in Cakephp

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



Leave a reply



Submit