Exemplo n.º 1
0
$app['debug'] = true;
$app = new Silex\Application();
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views'));
session_start();
if (empty($_SESSION['list_of_cars'])) {
    $porsche = new Car("2014 Porsche 911", 114991, 7864, "images/porsche.jpg");
    $ford = new Car("2011 Ford F450", 55995, 14241, "images/ford.jpg");
    $lexus = new Car("2013 Lexus RX 350", 44700, 20000, "images/lexus.jpg");
    $mercedes = new Car("Mercedes Benz CLS550", 39900, 37979, "images/mercedes.jpg");
    $cars = array($porsche, $ford, $lexus, $mercedes);
    $_SESSION['list_of_cars'] = $cars;
}
$app->get("/", function () use($app) {
    $cars = $_SESSION['list_of_cars'];
    return $app['twig']->render('cars.html.twig', array('cars' => $cars));
});
$app->post("/create_cars", function () use($app) {
    $car = new Car($_POST['make'], $_POST['miles'], $_POST['price'], $_POST['image']);
    $car->save();
    return $app['twig']->render('create_cars.html.twig', array('car' => $car));
});
$app->post("/search_car", function () use($app) {
    $cars_matching_search = array();
    foreach ($_SESSION['list_of_cars'] as $car) {
        if ($car->worthBuying($_POST["price"], $_POST["miles"])) {
            array_push($cars_matching_search, $car);
        }
    }
    return $app['twig']->render('search_car.html.twig', array('cars_matching_search' => $cars_matching_search));
});
return $app;
Exemplo n.º 2
0
    $_SESSION['cars_list'] = array();
}
$app = new Silex\Application();
// $app['debug'] = true;
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views'));
$app->get("/", function () use($app) {
    return $app['twig']->render('car_form.html.twig');
});
$app->post("/show", function () use($app) {
    $car = new Car($_POST['model'], $_POST['price'], $_POST['image'], $_POST['miles']);
    $car->save();
    return $app['twig']->render('car_dealership.html.twig', array('cars' => Car::getAll()));
});
$app->get("/list", function () use($app) {
    return $app['twig']->render('car_dealership.html.twig', array('cars' => Car::getAll()));
});
$app->get("car_search", function () use($app) {
    $cars = Car::getAll();
    $cars_matching_search = array();
    foreach ($cars as $car) {
        if ($car->worthBuying($_GET["price"]) && $car->maxMileage($_GET["mileage"])) {
            array_push($cars_matching_search, $car);
        }
    }
    return $app['twig']->render('car_search.html.twig', array('cars' => $cars_matching_search));
});
$app->post("/clear", function () use($app) {
    Car::reset();
    return $app['twig']->render('car_dealership.html.twig', array('cars' => Car::getAll()));
});
return $app;