Am new to slim i need help

Hi good people. as above am new to slim framework and would really appreciate if you guys help me the following effect as in the code below.

$app = new \Slim\App();

$app->get(’/’, function() use($app){
** $app->redirect(‘index.html’);**
});

$app->get(’/contact’, function() use($app){
** $app->render(‘contact.html’);**
});

$app->run();
Am trying to make index.html load by default if the url to my sample site is keyed in the browser. Mind you i have also index.php maybe a picture would help.!

Welcome @champ :slight_smile:

I would recommend to read the Slim Tutorial first.

You can use Twig or plain old PHP in Slim: Views and Templates.

$app->get('/', function (Request $request, Response $response) {
    $response = $this->view->render($response, 'index.html.php');

    return $response;
});

For security reasons the front controller file (index.php) should be placed in a sub-directory e.g. public/index.php + public/.htaccess. Read more.

Last but not least: use ($app) is not necessary.

Use $this->... within the callback function instead.

Example:

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app->get('/', function (Request $request, Response $response): Response {
    /* @var \Slim\App $this */
    /* @var \Slim\Views\Twig $view */
    $view = $this->getContainer()->get('view');

    $viewData = [];
    
    return $view->render($response, 'index.twig', $viewData);
});
1 Like

Hi.

Codecourse also have good tutorials using slim