failed url rewrite slim iis 10

Asked

Viewed 126 times

1

I’m trying to follow the slim tutorial here, but always give page not found when localhost:8080/hello/Alan access.

I’m using iis 10. I installed rewrite url 2.0, and my web.config looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="slim" patternSyntax="Wildcard">
                    <match url="*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

The directory structure of my application is:

.

project

src

public

The root of the site is the public folder and in it are the index.php and web.config files.

The code of index.php is:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;
$app->get('hello/{name}', function(Request $request, Response $response, array $args){
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");

    return $response;
});

$app->run();

I looked for help on the Internet and got no answer.

1 answer

0

The only problem is that you forgot to put the bar before your hello method. You put :$app->get('hello/{name}' instead of $app->get('/hello/{name}'

Your code should look like this:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;
$app->get('/hello/{name}', function(Request $request, Response $response, array $args){
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");

    return $response;
});

$app->run();

Obs.: No need to put function(Request $request, Response $response, array $args) pode usar diretoFunction($request, $sponse, $args)`

A more optimized way of this code would be:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;


$app->get('/hello/{name}', function ($request, $response, $args) {
    return $response->write('Hello: ' . $args['name']
    );
});

$app->run();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.