0% found this document useful (0 votes)
42 views10 pages

AngularJs Ngresource SlimPhp

The document contains code for an AngularJS web application that manages projects. It includes HTML, JavaScript, and configuration files. The HTML defines the layout and links to the AngularJS and other third party libraries. The JavaScript defines Angular modules, services, controllers and routes for displaying a list of projects, viewing project details, and adding new projects.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
42 views10 pages

AngularJs Ngresource SlimPhp

The document contains code for an AngularJS web application that manages projects. It includes HTML, JavaScript, and configuration files. The HTML defines the layout and links to the AngularJS and other third party libraries. The JavaScript defines Angular modules, services, controllers and routes for displaying a list of projects, viewing project details, and adding new projects.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

index.

html

<!doctype html>
<html ng-app="projects">
<head>
<meta charset="utf-8">
<title ng-bind="title" ng-cloak>Restaurant &mdash;</title>
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">
</head>

<body ng-controller="ProjectListCtrl">
<a class="brand" href="#">Projects Manager</a>

<div id="app-container" class="container-fluid">
<div class="row-fluid">

<div class="span12" id="main" ng-view>

</div><!--/.span12-->
</div><!--/.row-fluid-->
<footer>Copyright Projects &copy; 2013</footer>
</div><!--/.container-->


<script src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js">
</script>

<!-- Don't forget to load angularjs AND angular-resource.js -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.js">
</script>


<!--Controllers-->
<script src="app.js"></script>
</body>
</html>

app.js
var projectsApp = angular.module('projects', ['ngResource']);

projectsApp.config(function($routeProvider) {
$routeProvider.
when('/',
{
controller: 'ProjectListCtrl',
templateUrl: 'projectlist.html'
}
).
when('/detail/:id',
{
controller: 'ProjectDetailCtrl',
templateUrl: 'template/projectdetail.html'
}).
when('/add/:id',
{
controller: 'ProjectDetailCtrl',
templateUrl: 'template/projectAdd.html'
}).
otherwise('/');
});


projectsApp.factory( 'RestFull', [
'$resource', function( $resource ) {
return function( url, params, methods ) {
var defaults = {
update: { method: 'put', isArray: false },
create: { method: 'post' }
};

methods = angular.extend( defaults, methods );

var resource = $resource( url, params, methods );

resource.prototype.$save = function(f) {
if ( !this.id ) {
this.$create(f);
}
else {
this.$update(f);
}
};

return resource;
};
}
]
);




projectsApp.factory( 'Project', [ 'RestFull', function( $resource ) {


return $resource( 'rest/projects/:id', {} );
//return $resource( 'rest/projects/:id', { id: '@id' } );
}
]
);


projectsApp.controller('ProjectListCtrl', function(Project, $scope) {

Project.query(
function(data)
{
$scope.projects = data;
console.log("Proyectos:");
console.log($scope.projects);
console.log("----------------------");
}
);


$scope.delete = function(pro, index)
{
pro.$delete({id: pro.id},
function()
{
$scope.projects.splice(index, 1);
}
);
}

});



projectsApp.controller('ProjectDetailCtrl', function(Project, $routeParams, $scope, $location) {

$scope.project = $routeParams.id
? Project.get({id: $routeParams.id})
: new Project();

$scope.save = function()
{
$scope.project.$save(
function(p){
console.log("Save or UpDate Project");
console.log(p);
//$scope.projects.push(p);
console.log("----------------------");
$location.path('/');
}
);
}

});

index.php
<?php

require 'Slim/Slim.php';

$app = new Slim();

$app->get('/', 'index');
$app->get('/projects', 'getProjects');
$app->get('/projects/:id', 'getProject');
$app->post('/projects', 'addProject');
$app->put ('/projects', 'updateProject');
$app->delete('/projects/:id', 'delProject');

$app->run();


function index() {

$request = <<<EOT

<h1>index<h1/>

EOT;


}






echo $request;

function getProjects() {

$sql = "select * from projects";

try {

$db = getConnection();

$stmt = $db->query($sql);

$projects = $stmt->fetchAll(PDO::FETCH_OBJ);

$db = null;

echo json_encode($projects);

} catch(PDOException $e) {

echo '{"error":{"text":'. $e->getMessage() .'}}';


}

function getProject($id) {

$sql = "select * from projects WHERE id=:id;";

try {

$db = getConnection();

$stmt = $db->prepare($sql);

$stmt->bindParam("id", $id);

$stmt->execute();

$project = $stmt->fetchObject();

$db = null;

echo json_encode($project);

} catch(PDOException $e) {

echo '{"error":{"text":'. $e->getMessage() .'}}';


function addProject() {

$request = Slim::getInstance()->request();

$project = json_decode($request->getBody());

$sql = "INSERT INTO projects (name, phone) VALUES (:name, :phone)";

try {

$db = getConnection();

$stmt = $db->prepare($sql);

$stmt->bindParam("name", $project->name);

$stmt->bindParam("phone", $project->phone);

$stmt->execute();

$project->id = $db->lastInsertId();

$db = null;

echo json_encode($project);

} catch(PDOException $e) {

echo '{"error":{"text":'. $e->getMessage() .'}}';

}


function updateProject() {

$request = Slim::getInstance()->request();

$body = $request->getBody();

$project = json_decode($body);

$sql = "UPDATE projects SET name=:name, phone=:phone WHERE id=:id";

try {

$db = getConnection();

$stmt = $db->prepare($sql);

$stmt->bindParam("name", $project->name);

$stmt->bindParam("phone", $project->phone);

$stmt->bindParam("id", $project->id);

$stmt->execute();

$db = null;

echo json_encode($project);

} catch(PDOException $e) {

echo '{"error":{"text":'. $e->getMessage() .'}}';

}


function delProject($id) {

$sql = "DELETE FROM projects WHERE id=:id";

try {

$db = getConnection();

$stmt = $db->prepare($sql);

$stmt->bindParam("id", $id);

$stmt->execute();

$db = null;

} catch(PDOException $e) {

echo '{"error":{"text":'. $e->getMessage() .'}}';

function getConnection() {

$dbhost="127.0.0.1";

$dbuser="root";

$dbpass="root";

$dbname="db_prueba";

$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

return $dbh;

}

?>

You might also like