Ir para o conteúdo
  • Revista PROGRAMAR: Já está disponível a edição #59 da revista programar. Faz já o download aqui!

sEnte

Slim Framework...

Mensagens Recomendadas

sEnte    0
sEnte

Estou novamente com problema numa app que estou a desenvolver e estou a utilizar a framework Slim.

Indo por partes...

O que acontece é o seguinte.

Tenho uma listagem de vários produtos e quando clico num é suposto adicionar a um cesto de compras e essa parte funciona, o que não funciona é a parte em que quando coloco na barra de endereço um produto que não existe deveria voltar ao index, no entanto dá erro.

CartController.php

<?php
 namespace Cart\Controllers;
 use Slim\Router;
 use Slim\Views\Twig;
 use Cart\Basket\Basket;
 use Cart\Models\Product;
 use Psr\Http\Message\ResponseInterface as Response;
 use Psr\Http\Message\ServerRequestInterface as Request;
 class CartController {
protected $basket;
protected $product;
public function __construct(Basket $basket, Product $product) {
  $this->basket = $basket;
  $this->product = $product;
}
public function index(Request $request, Response $response, Twig $view) {
  return $view->render($response, 'cart/index.twig');
}
public function add($slug, $quantity, Request $request, Response $response, Router $router) {
  $product = $this->product->where('slug', $slug)->first();
  if (!$product) {
	return $response->withRedirect($router->pathFor('home'));
  }
  die('add');
}
 }
?>

Basket.php

<?php
 namespace Cart\Basket;
 use Cart\Models\Product;
 use Cart\Support\Storage\Contracts\StorageInterface;
 use Cart\Basket\Exceptions\QuantityExceededException;
 class Basket {

protected $storage;
protected $product;
public function __construct(StorageInterface $storage, Product $product) {
  $this->storage = $storage;
  $this->product = $product;
}
public function add(Product $product, $quantity) {
  if ($this->has($product)) {
	$quantity = $this->get($quantity)['quantity'] + $quantity;
  }
  $this->update($product, $quantity);
}
public function update(Product $product, $quantity) {
  if (!$this->product->find($product->id)->hasStock($quantity)) {
	throw new QuantityExceededException;

  }
  if ($quantity === 0) {
	$this->remove($product);
	return;
  }
  $this->storage->set($product->id, [
	'product_id' => (int) $product->id,
	'quantity' => (int) $quantity,
  ]);
}
public function remove(Product $product) {
   $this->storage->unset($product->id);
}
public function has(Product $product) {
  return $this->storage->exists($product->id);
}
public function get(Product $product) {
  return $this->storage->get($product->id);
}
public function clear() {
  $this->storage->clear();
}
public function all() {
  $ids = [];
  $items = [];
  foreach ($this->storage->all() as $product) {
	$ids[] = $product['product_id'];
  }
  $products = $this->product->find($ids);
  foreach ($products as $product) {
	$product->quantity = $this->get($product)['quantity'];
	$items[] = $product;
  }
  return $items;
}
public function itemCount() {
  return count($this->storage);
}
 }
?>

Quando clico num dos elementos a adicionar aparece uma página com informação como deveria aparecer. Aí quando altero o endereço para um produto que não existe ele retorna ao index. Por outro lado quando clico no produto e faço "Adicionar" ele adiciona e adiciona uma quantidade por defeito que é 1. Aqui quando tento modificar o endereço para um produto inexistente dá-me erro.

esta é a parte final do log de erros.

[Wed Jun 15 00:53:36.203942 2016] [mpm_prefork:notice] [pid 1008] AH00170: caught SIGWINCH, shutting down gracefully
[Thu Jun 16 20:38:53.531647 2016] [core:notice] [pid 1036] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Thu Jun 16 20:38:53.791840 2016] [suexec:notice] [pid 1036] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Thu Jun 16 20:38:53.807760 2016] [auth_digest:notice] [pid 1036] AH01757: generating secret for digest authentication ...
[Thu Jun 16 20:38:53.808023 2016] [http2:warn] [pid 1036] AH02951: mod_ssl does not seem to be enabled
[Thu Jun 16 20:38:53.808541 2016] [lbmethod_heartbeat:notice] [pid 1036] AH02282: No slotmem from mod_heartmonitor
[Thu Jun 16 20:38:58.177760 2016] [mpm_prefork:notice] [pid 1036] AH00163: Apache/2.4.18 (Fedora) PHP/5.6.22 configured -- resuming normal operations
[Thu Jun 16 20:38:58.177783 2016] [core:notice] [pid 1036] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Thu Jun 16 20:38:58.421798 2016] [:warn] [pid 1199] ./mod_dnssd.c: No services found to register
[Thu Jun 16 20:45:32.998298 2016] [:error] [pid 1200] [client 127.0.0.1:44440] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening <?php tag?' in /var/www/html/cart/vendor/slim/slim/Slim/App.php:552\nStack trace:\n#0 /var/www/html/cart/vendor/slim/slim/Slim/App.php(344): Slim\\App->finalize(Object(Slim\\Http\\Response))\n#1 /var/www/html/cart/vendor/slim/slim/Slim/App.php(298): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#2 /var/www/html/cart/public/index.php(5): Slim\\App->run()\n#3 {main}\n  thrown in /var/www/html/cart/vendor/slim/slim/Slim/App.php on line 552
[Thu Jun 16 20:47:02.873741 2016] [:error] [pid 1201] [client 127.0.0.1:44566] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening <?php tag?' in /var/www/html/cart/vendor/slim/slim/Slim/App.php:552\nStack trace:\n#0 /var/www/html/cart/vendor/slim/slim/Slim/App.php(344): Slim\\App->finalize(Object(Slim\\Http\\Response))\n#1 /var/www/html/cart/vendor/slim/slim/Slim/App.php(298): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#2 /var/www/html/cart/public/index.php(5): Slim\\App->run()\n#3 {main}\n  thrown in /var/www/html/cart/vendor/slim/slim/Slim/App.php on line 552, referer: http://127.0.0.1/cart/public/products/bolivia-la-linda-washed-longberry
[Thu Jun 16 20:47:06.453515 2016] [:error] [pid 1201] [client 127.0.0.1:44566] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening <?php tag?' in /var/www/html/cart/vendor/slim/slim/Slim/App.php:552\nStack trace:\n#0 /var/www/html/cart/vendor/slim/slim/Slim/App.php(344): Slim\\App->finalize(Object(Slim\\Http\\Response))\n#1 /var/www/html/cart/vendor/slim/slim/Slim/App.php(298): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#2 /var/www/html/cart/public/index.php(5): Slim\\App->run()\n#3 {main}\n  thrown in /var/www/html/cart/vendor/slim/slim/Slim/App.php on line 552, referer: http://127.0.0.1/cart/public/
[Thu Jun 16 20:47:17.536169 2016] [:error] [pid 1201] [client 127.0.0.1:44566] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening <?php tag?' in /var/www/html/cart/vendor/slim/slim/Slim/App.php:552\nStack trace:\n#0 /var/www/html/cart/vendor/slim/slim/Slim/App.php(344): Slim\\App->finalize(Object(Slim\\Http\\Response))\n#1 /var/www/html/cart/vendor/slim/slim/Slim/App.php(298): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#2 /var/www/html/cart/public/index.php(5): Slim\\App->run()\n#3 {main}\n  thrown in /var/www/html/cart/vendor/slim/slim/Slim/App.php on line 552
[Thu Jun 16 20:50:22.028374 2016] [:error] [pid 1203] [client 127.0.0.1:44642] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening <?php tag?' in /var/www/html/cart/vendor/slim/slim/Slim/App.php:552\nStack trace:\n#0 /var/www/html/cart/vendor/slim/slim/Slim/App.php(344): Slim\\App->finalize(Object(Slim\\Http\\Response))\n#1 /var/www/html/cart/vendor/slim/slim/Slim/App.php(298): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#2 /var/www/html/cart/public/index.php(5): Slim\\App->run()\n#3 {main}\n  thrown in /var/www/html/cart/vendor/slim/slim/Slim/App.php on line 552
[Thu Jun 16 20:51:25.455605 2016] [:error] [pid 1206] [client 127.0.0.1:44666] PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening <?php tag?' in /var/www/html/cart/vendor/slim/slim/Slim/App.php:552\nStack trace:\n#0 /var/www/html/cart/vendor/slim/slim/Slim/App.php(344): Slim\\App->finalize(Object(Slim\\Http\\Response))\n#1 /var/www/html/cart/vendor/slim/slim/Slim/App.php(298): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))\n#2 /var/www/html/cart/public/index.php(5): Slim\\App->run()\n#3 {main}\n  thrown in /var/www/html/cart/vendor/slim/slim/Slim/App.php on line 552

Editado por sEnte

"If It Ain't Broke, Break it and build something Cooler!" Unknown

Partilhar esta mensagem


Ligação para a mensagem
Partilhar noutros sites
sEnte    0
sEnte

E no CartController.php na função add que não está a fazer o redirecionamento

Editado por sEnte

"If It Ain't Broke, Break it and build something Cooler!" Unknown

Partilhar esta mensagem


Ligação para a mensagem
Partilhar noutros sites
sEnte    0
sEnte

se mandar guardar o CartController com BOM dá-me o erro de namespace has to be the very first statement. Se for em utf8 continua na mesma.


"If It Ain't Broke, Break it and build something Cooler!" Unknown

Partilhar esta mensagem


Ligação para a mensagem
Partilhar noutros sites
HappyHippyHippo    1153
HappyHippyHippo

podes confirmar que isto está bem ?

$this->product->where('slug', $slug)->first();

se estiver a mandar alguma mensagem de erro para o output é o suficiente para dar esse erro


IRC : sim, é algo que ainda existe >> #p@p

Partilhar esta mensagem


Ligação para a mensagem
Partilhar noutros sites

Crie uma conta ou ligue-se para comentar

Só membros podem comentar

Criar nova conta

Registe para ter uma conta na nossa comunidade. É fácil!

Registar nova conta

Entra

Já tem conta? Inicie sessão aqui.

Entrar Agora

×

Aviso Sobre Cookies

Ao usar este site você aceita os nossos Termos de Uso e Política de Privacidade. Este site usa cookies para disponibilizar funcionalidades personalizadas. Para mais informações visite esta página.