Last active
August 30, 2016 20:41
-
-
Save tmiele/542e0d491d3066874b06c498ef7285fc to your computer and use it in GitHub Desktop.
AngularJS Todo list with Slim 3 PHP server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> | |
</head> | |
<body ng-app="myApp" > | |
<header ng-controller="JeanCtrl as jean"> | |
<h1>Hello {{jean.name}}</h1> | |
</header> | |
<article ng-controller="ArthurCtrl"> | |
<input type="text" ng-model="todo"> | |
<button ng-click="add()">Add</button> | |
<button ng-click="pop()">Pop</button> | |
<button ng-click="send()">Send</button> | |
<button ng-click="call()">json</button> | |
<ul> | |
<li ng-repeat="x in records"> | |
{{x}} <button ng-click="removeItem($index)">X</button> | |
</li> | |
</ul> | |
</article> | |
<script type="application/javascript"> | |
var app = angular.module("myApp", []); | |
app.controller("ArthurCtrl", function($scope, $rootScope, $http) { | |
$scope.todo = ''; | |
$scope.records = [ | |
"Nicolas Tesla", | |
"Albert Einstein", | |
"Alan Turing", | |
"Linus Torvalds" | |
]; | |
$scope.add = function() { | |
$scope.records.push($scope.todo); | |
$scope.todo = ''; | |
} | |
$scope.pop = function() { | |
$scope.records.pop(); | |
} | |
$scope.removeItem = function(index) { | |
$scope.records.splice(index, 1); | |
} | |
$scope.send = function() { | |
$rootScope.$broadcast('Arthur:todo', {name: $scope.todo}); | |
} | |
$scope.call = function() { | |
$http.get('/api/home').then(function(response) { | |
console.log(response); | |
console.log(response.headers()); | |
$scope.json = response.data; | |
}); | |
} | |
}); | |
/** | |
* Using controller as syntax, remove $scope dependency | |
*/ | |
app.controller('JeanCtrl', function($rootScope) { | |
var self = this; | |
self.name = "Thomas"; | |
$rootScope.$on('Arthur:todo', function (event, data) { | |
self.name = data.name; | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"slim/slim": "^3.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \Psr\Http\Message\ServerRequestInterface as Request; | |
use \Psr\Http\Message\ResponseInterface as Response; | |
require 'vendor/autoload.php'; | |
function Artichaut($request, $response, $next) { | |
// auth bad | |
if (false) { | |
$data = ['success' => false, 'msg' => 'JWT error', 'token' => '']; | |
$response = $response->withJson($data, 404); | |
return $response; | |
} | |
$response = $next($request, $response); | |
return $response; | |
} | |
function Coeur($request, $response, $next) { | |
$response = $next($request, $response); | |
$json = json_decode($response->getBody()); | |
$json->Coeur = 'Artichaut'; | |
$response->withJson($json); | |
$response = $response->withHeader('Coeur-Artichaut', 'Coeur-Artichaut'); | |
return $response; | |
} | |
$app = new \Slim\App; | |
$app->get('/', function (Request $request, Response $response) { | |
$response->getBody()->write('Hello World !<br><a href="app.html">App</a>'); | |
return $response; | |
}); | |
$app->get('/home', function (Request $request, Response $response) { | |
$datas = array( | |
'barcode' => 'Q-007', | |
'email' => '[email protected]', | |
'points' => 42 | |
); | |
$response->withJson($datas); | |
return $response; | |
}); | |
$app->group('/api', function() use ($app) { | |
$app->get('/home', function (Request $request, Response $response) { | |
$datas = array( | |
'barcode' => 'Q-007', | |
'email' => '[email protected]', | |
'points' => 42 | |
); | |
$response->withJson($datas); | |
return $response; | |
}); | |
$app->get('/about', function (Request $request, Response $response) { | |
$response->getBody()->write("<pre> by Doing SARL</pre>"); | |
return $response; | |
}); | |
}) | |
->add(Coeur)->add(Artichaut); | |
// ->add(Artichaut)->add(Coeur); | |
// Si Coeur->Artichaut OK | |
// Si Artichaut->Coeur !! inverser le coeur se retrouve en coquille | |
// En oignon le dernier add sera le premier traverser en entrer et le dernier en sortie | |
// artichaut => coeur => slim => coeur => artichaut | |
$app->run(); | |
/* | |
# Doing | |
.htaccess | |
``` | |
RewriteEngine On | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^ index.php [QSA,L] | |
``` | |
Makefile | |
``` | |
all: | |
php -S localhost:8080 -t . | |
``` | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment