Getting started with Slim PHP framework by building a very simple MVC/OOP app ... We are going to build a Slim application that creates a farm pen and outputs the noises that the cow & chicken ... Must implement an MVC approach (help us understand how models, views and controllers work together)
Aim:
We are going to build a Slim application that creates a farm pen and outputs the noises that the cow & chicken inside the pen make.
Requirements:
1. So we are gonna need the slim skeleton application - open your command line and run:
composer create-project slim/slim-skeleton slimFarm
2. On your command line navigate (cd) into slimFarm directory that has been made for you
3. We are going to run a php server so we can see that the slim app is present and working
php -S 0.0.0.0:8080 -t ./public/
4. Go to http://0.0.0.0:8080/ to check its working (you should get the slim default page)
5. Open files in your text editor and make a Classes folder inside src
6. Add your namespace to autoloading in composer.json:
"autoload": { "psr-4": { "Farm\\": "src/classes/" } },
7. Open a new tab in your command line and run composer dump-autoload
in the command line
8. We need our Cow and Chickens to go in the pen! so make our classes inside Classes/Models folder:
namespace Farm\Models;class ChickenModel
{
public $speak = ‘cluck’;
}namespace Farm\Models;
class CowModel
{
public $speak = ‘moo’;
}
9. Create a Pen class inside Classes/Models folder:
namespace Farm\Models;
class PenModel
{
public $cow;
public $chicken;public function __construct($cow, $chicken) { $this->cow = $cow; $this->chicken = $chicken; } public function getCowNoise(){ return $this->cow->speak; } public function getChickenNoise(){ return $this->chicken->speak; }
}
10. Lets make a factory so that Pen can be created quickly with its dependencies in just one call.
namespace Farm\Factories;class PenModelFactory
{
function __invoke()
{
$cow = new \Farm\Models\CowModel();
$chicken = new \Farm\Models\ChickenModel();
return new \Farm\Models\PenModel($cow, $chicken);
}
}
11. So now we have made our factory let’s put it in our DIC
$container[‘penModel’] = new \Farm\Factories\PenModelFactory();
12. Let’s look in our routing file to see how we deal with different HTTP Requests to our application.
$app->get(‘/makeMeAPen’, <where our callback will go> );
13. Let’s make a controller to call our PenFactory and render out a view:
namespace Farm\Controllers;class PenController
{
protected $container;//this constructor passes the DIC in so we can get our PenFactory out of it later function __construct($container) { $this->container = $container; } function __invoke($request, $response, $args) { //create our pen from Penfactory in DIC $pen = $this->container->get('penModel'); //assign args (variables that will be available on rendered view) $args['cowNoise'] = $pen->getCowNoise(); $args['chickenNoise'] = $pen->getChickenNoise(); //get the default template renderer out of DIC and pass the response and $args to a template file return $this->container->get('renderer')->render($response, 'showFarm.phtml', $args); }
}
14. Now our controller exists lets add it in as the thing that happens when our route is hit:
$app->get(‘/makeMeAPen’, \Farm\Controllers\PenController::class);
15. Notice in our PenController we are calling a template file that doesn’t exist yet so lets create it:
<!DOCTYPE html>
<html>
<head>
<title>slimFarm</title>
<style>
h2 {
font-size: 10rem;
color: red;
}
</style>
</head>
<body>
<h2>Cows go: <?php echo htmlspecialchars($cowNoise); ?>!</h2>
<h2>Chickens go: <?php echo htmlspecialchars($chickenNoise); ?>!</h2>
</body>
</html>
16. Navigate to http://0.0.0.0:8080/makeMeAPen and behold the brilliance of your super advanced app!
#php