¿How to do this in slim 3?

$app->get('/paste/nuevo/', function() use ($app) {
	$app->render('crear.php');
});
$app->post('/paste/nuevo/', function() use ($app) {
	$request = $app->request;
	$nombre = $request->post('nombre');
	$contenido = $request->post('contenido');
	$url = $request->post('url');
	$conectar = mysqli_connect('localhost', 'root', '', 'paste');
	$insertar = mysqli_query($conectar, "insert into pastes set p_nombre='$nombre', p_contenido='$contenido', p_url='$url' ");
	$app->redirect('../nuevo/');
});

I only use version 2.6

The code can be translated to Slim 3 as follows:

<?php
require_once __DIR__ . '/../vendor/autoload.php';

// Create app and register PhpView
$app = new \Slim\App([
    'view' => function ($container) {
        return new \Slim\Views\PhpRenderer(__DIR__ . '/../views/');
    }
]);

$app->get('/paste/nuevo/', function($request, $response) {
    return $this->view->render($response, 'crear.php');
})->setName('paste_nuevo');

$app->post('/paste/nuevo/', function($request, $response) {
    $body = $request->getParsedBody();

    $nombre = $body['nombre'];
    $contenido = $body['contenido'];
    $url = $body['url'];

    // database insert left out

    return $response->withRedirect($this->router->pathFor('paste_nuevo'));
});

$app->run();

I have left out the database insertion. There is an example of working with databases in the tutorial (see links below).

Here are some links that you may find helpful:

1 Like