8

I'm new to Slim Framework. How to get the base URL like with the Codeigniter function base_url()?

Thanks

claire_
  • 723
  • 2
  • 8
  • 17

7 Answers7

18

You need to set the base url manually FIRST before you can get it as in this:

$app->hook('slim.before', function () use ($app) {
    $app->view()->appendData(array('baseUrl' => '/base/url/here'));
});

http://help.slimframework.com/discussions/questions/49-how-to-deal-with-base-path-and-different-routes

Dreaded semicolon
  • 2,274
  • 1
  • 28
  • 43
14

With Slim v3, as it implements PSR7, you can get a PSR7 Uri object and call the getBasePath() method that Slim3 adds on it. Simply write:

$basePath = $request->getUri()->getBasePath();

From Slim v3 documentation :

Base Path

If your Slim application's front-controller lives in a physical subdirectory beneath your document root directory, you can fetch the HTTP request's physical base path (relative to the document root) with the Uri object's getBasePath() method. This will be an empty string if the Slim application is installed in the document root's top-most directory.

Be aware that the getBasePath() method is added by the framework and is not part of PSR7 UriInterface.

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
  • there is no such method `getBasePath()` in the PSR-7 Uri interface. – Stefano Torresi Nov 23 '17 at 18:04
  • @StefanoTorresi : Maybe you misunderstood what I badly expressed (english is not my mother tongue): Slim3 offers a getBasePath() over the PSR7 Uri object. I never wrote this method is part of PSR7 Uri interface. Please refer to Slim3 docs. – Al Foиce ѫ Nov 24 '17 at 09:56
  • I understood what you meant, but yes, as you admit yourself it's expressed incorrectly: the `getBasePath()` method is specific of `Slim\Http\Uri`, it is not PSR-7 compliant. Given that the rest of the documentation suggests, and rightfully so, to hint against PSR-7 interfaces, using the implementation specific method will result in a vendor lock-in and will compromise the portability that PSR-7 support was meant to give in the first place. To clarify your answer, I would suggest to drop the PSR-7 reference, because as it is now it implies that the suggested method is part of the standard. – Stefano Torresi Nov 24 '17 at 10:28
  • Sorry for being late to the party but I think currently the right method is `getBaseUrl()` – Ivan of uganda Feb 22 '19 at 02:08
6

In a recent app where we're using Twig, we assign the httpBasePath as follows:

$view = $app->view()->getEnvironment();
$view->addGlobal('httpBasePath', $app->request->getScriptName());

The addGlobal() method is probably equivalent to $app->view()->appendData(), I'm not sure.

The advantage of using $app->request->getScriptName() is that we don't have to manually set a folder name or care what it is – one developer can have the repo located at http://example.localhost and another can have it at http://localhost/projects/slim and no configuration is required.

Charlie Schliesser
  • 7,851
  • 4
  • 46
  • 76
4

Try this in index.php to set the base url for the view

$app->hook('slim.before', function () use ($app) {
    $posIndex = strpos( $_SERVER['PHP_SELF'], '/index.php');
    $baseUrl = substr( $_SERVER['PHP_SELF'], 0, $posIndex);
    $app->view()->appendData(array('baseUrl' => $baseUrl ));
});
qwertz
  • 1,894
  • 17
  • 13
  • This reminds me of the same function in WordPress, this should be included in the "basics" of the framework documentation. – Rodrigo Polo Jun 12 '14 at 07:53
2

I can get the base url with {{ app.request.getRootUri }} (I'm using Twig template engine). Incidentally, this is the same as the 'SCRIPT_NAME' environment variable.

1

if you are using TWIG then in Slim v3 call -

{{ base_url() }}

or use {{ path_for('yourRouteName') }}

HADI
  • 2,829
  • 1
  • 26
  • 26
0

The easiest way to get the base url is to append the request url and the request root url like below: $req = $app->request; $base_url = $req->getUrl()."".$req->getRootUri()."/";

Mutinda Boniface
  • 518
  • 1
  • 5
  • 14