Laravel: Obtener url base
Pregunta simple, pero la respuesta parece bastante difícil de encontrar. En Codeigniter, podría cargar el ayudante de url y luego simplemente hacer
echo base_url();
Para obtener la URL de mi sitio. Hay un equivalente en Laravel?
12 answers
Puede usar la fachada URL que le permite hacer llamadas al generador de URL
Así que puedes hacer:
URL::to('/');
También puede usar el contenedor de la aplicación:
$app->make('url')->to('/');
$app['url']->to('/');
App::make('url')->to('/');
O inyectar el UrlGenerator:
<?php
namespace Vendor\Your\Class\Namespace;
use Illuminate\Routing\UrlGenerator;
class Classname
{
protected $url;
public function __construct(UrlGenerator $url)
{
$this->url = $url;
}
public function methodName()
{
$this->url->to('/');
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-17 10:31:00
Laravel
echo url();
Laravel >= 5.2
echo url('/');
Espero que esto te ayude
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-07 22:03:48
Para Laravel 5 normalmente uso:
<a href="{{ url('/path/uri') }}">Link Text</a>
Entiendo que usar la función url()
es llamar al mismo Facade
que URL::to()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-30 23:44:45
Laravel proporciona un montón de funciones auxiliares y para su requerimiento puede simplemente
Use url () función de los ayudantes de Laravel
Pero en el caso de Laravel 5.2, usted tendrá que usar url('/')
Aquí está la lista de todas las demás funciones auxiliares de Laravel
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-20 13:52:54
Para que funcione con URLs no bonitas tuve que hacer:
asset('/');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-04 04:38:52
Esto:
echo url('/');
Y esto:
echo asset('/');
Ambos mostraban la url de inicio en mi caso:)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-11 02:07:59
Otra posibilidad: {{ URL::route('index') }}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-30 17:50:58
Para obtener la url de la aplicación que configuró, puede usar Config::get('app.url")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-01 00:12:59
Usé esto y funcionó para mí en Laravel 5.3.18:
<?php echo URL::to('resources/assets/css/yourcssfile.css') ?>
NOTA IMPORTANTE: Esto solo funcionará cuando ya hayas eliminado "público" de tu URL. Para hacer esto, puede consultar este útil tutorial.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-17 10:31:45
También puede usar URL::to('/') para mostrar la imagen en Laravel. Véase a continuación:
<img src="{{URL::to('/')}}/images/{{ $post->image }}" height="100" weight="100">
Supongamos que su imagen se almacena en "public/images".
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-11 09:17:19
Por cierto, si tu ruta tiene un nombre como:
Route::match(['get', 'post'], 'specialWay/edit', 'SpecialwayController@edit')->name('admin.spway.edit');
Puedes usar la función route()
así:
<form method="post" action="{{route('admin.spway.edit')}}" class="form form-horizontal" id="form-spway-edit">
Otras funciones útiles:
$uri = $request->path();
$url = $request->url();
$url = $request->fullUrl();
asset()
app_path();
// ...
Https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/helpers.php
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-27 09:34:57
Puede usar fachadas o función auxiliar de la siguiente manera.
echo URL::to('/');
echo url();
Laravel usando el componente Symfony para la solicitud, la lógica interna de Laravel según siguiente.
namespace Symfony\Component\HttpFoundation;
/**
* {@inheritdoc}
*/
protected function prepareBaseUrl()
{
$baseUrl = $this->server->get('SCRIPT_NAME');
if (false === strpos($this->server->get('REQUEST_URI'), $baseUrl)) {
// assume mod_rewrite
return rtrim(dirname($baseUrl), '/\\');
}
return $baseUrl;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-03 10:52:53