0

With the PHP framework Slim 3 in Routes, I did this:

// In routes :
$app->get('article/{id}-{slug}', function ($request, $response, $args) {
    $class = new Site\ArticleController($args);
    $class->show();
});

// In controllers :
public function show($args)
{
    $sql = "SELECT * FROM articles WHERE id = $args['id'] AND slug = $args['slug']";
    // ...
}

In Laravel 5 this coudl be written like:

// In routes :
Route::get('article/{id}-{slug}', 'Site\ArticleController@show');

// In controllers :
public function show($id, $slug)
{
    $sql = "SELECT * FROM articles WHERE id = $id AND slug = $slug";
    // ...
}

Can we do the same with Slim 3? I mean this:

$app::get('article/{id}-{slug}', 'Site\ArticleController@show');
lorem monkey
  • 3,942
  • 3
  • 35
  • 49
stephweb
  • 125
  • 2
  • 16

1 Answers1

3

You can structure Slim 3 routes similar to Laravel by doing something like this:

<?php
// In routes :
$app->get('article/{id}-{slug}', '\Site\ArticleController:show');

// In controllers :
public function show($request, $response, $args)
{
    $sql = "SELECT * FROM articles WHERE id = $args['id'] AND slug = $args['slug']";
    // ...
}

The Slim router now passes $request and $response in the first and second parameters and then any Route arguments you set in the last $args.

I hope this helps! :)

Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
Devin McBeth
  • 428
  • 4
  • 10