The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

[Slim 3.x] Where is urlFor() / pathFor ?

iveinsomnia's Avatar

iveinsomnia

08 Sep, 2015 03:00 PM

Hi, I'm really terribly sorry to ask this but I search for hours now.
In Slim 2.x it was really easy to make urls, but I'm totally lost now :/

<?php
$app = new \Slim\Slim();

$app->get('/', function () use ($app) {
    echo "Welcome to Slim!";
})->name('home');

$url = $app->urlFor('home');

And for 3.x

<?php
$app = new Slim\App();

$app->get('/', function ($request, $response, $args) {
    $response->write("Welcome to Slim!");
    return $response;
})->setName('home');

But from here ether "$app->urlFor('home');" and "$app->pathFor('home');" will just throw an error "Fatal error: Call to undefined method Slim\App::urlFor()"

I tryed some tutorials on Slim3.x without any success, including "$app['router']->urlFor('home');" wich give me a "Fatal error: Cannot use object of type Slim\App as array"

Slim 2.x was so easy to use T.T

PS: Sorry for my English.

  1. 1 Posted by Rob Allen on 08 Sep, 2015 03:05 PM

    Rob Allen's Avatar

    Try this:

    $app = new \Slim\App();
    $router = $app->router;
    
    $app->get('/', function ($request, $response, $args) use ($router) {
        $url = $router->pathFor('home');
        $response->write("Welcome to Slim!");
        $response->write(" <a href='$url'>Go Home</a>");
        return $response;
    })->setName('home');
    
    $app->run();
    
  2. 2 Posted by Rob Allen on 08 Sep, 2015 03:17 PM

    Rob Allen's Avatar

    This is now documented in the Upgrade guide.

  3. 3 Posted by iveinsomnia on 08 Sep, 2015 03:28 PM

    iveinsomnia's Avatar

    Ho great thanks I was able to (almost) make it fine now :p

    <?= "{$router->pathFor('home')}"; ?>
    

    now return "/"

    [EDIT]

    $app = new Slim\App();
    $router = $app->router;
    $path = $app->environment['CONTEXT_PREFIX'];
    
    $app->get('/', function ($request, $response, $args) use ($router) {
        $response->write("Welcome to Slim!");
        return $response;
    })->setName('home');
    
    $app->run();
    

    <?= "{$path}{$router->pathFor('home')}"; ?>
    Return my right path "/MLF"+"/"

    Thank you very mutch sir :p

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac