This is Crudcontoller code
<?php
namespace Crud\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Service\PicManager;
use Zend\View\Model\ViewModel;
use Crud\Entity\Pic;
use Crud\Form\AddForm;
/**
* This controller is designed for managing image file uploads.
*/
class CrudController extends AbstractActionController
{
/**
* Entity manager.
* @var Doctrine\ORM\EntityManager
*/
private $entityManager;
/**
* Image manager.
* @var Application\Service\PicManager;
*/
private $picManager;
/**
* Constructor.
*/
public function __construct($entityManager, $picManager)
{
$this->entityManager = $entityManager;
$this->picManager = $picManager;
}
/**
* This is the default "index" action of the controller. It displays the
* Image Gallery page which contains the list of uploaded images.
*/
public function indexAction()
{
//echo 'in'; exit;
//$form = new AddForm();
//return new ViewModel(['form' => $form]);
// Get the list of already saved files.
//$files = $this->picManager->getSavedFiles();
$pic = $this->entityManager->getRepository(Pic::class)->findAll();
return new ViewModel(['pic' => $pic]);
}
/**
* This action shows the image upload form. This page allows to upload
* a single file.
*/
public function uploadAction()
{
// Create the form.
$form = new AddForm();
// Check whether this post is a POST request.
if ($this->getRequest()->isPost()) {
// Get POST data.
$data = $this->params()->fromPost();
// Fill form with data.
$form->setData($data);
if ($form->isValid()) {
// Get validated form data.
$data = $form->getData();}
// echo '<pre>';
// print_r($data);
//echo '</pre>';
//exit();
// Use post manager service to add new post to database.
$img_name = $data['file']['name'];
$url = $_SERVER['HTTP_SERVER'];
$seg= explode('/',$url);
$path = $seg[0].'/'.$seg[1].'/'.$seg[2].'/'.$seg[3];
$full_url = $path.'/'.'public'.'/'.'img'.'/'.$img_name;
$data['file']['tmp_name'] = $full_url;
$value = [
'name' => $data['file']['name'],
'avatar' => $data['file']['tmp_name'],
];
$this->picManager->addNewImage($data);
return $this->redirect()->toUrl('http://projecting.localhost/images');
}
return new ViewModel([
'form' => $form
]);
}
This is module.config.php
<?php
/**
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Crud;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\Router\Http\Regex;
use Zend\ServiceManager\Factory\InvokableFactory;
use Application\Route\StaticRoute;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'images' => [
'type' => Segment::class,
'options' => [
'route' => '/images[/:action]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
],
'defaults' => [
'controller' => Controller\CrudController::class,
'action' => 'upload',
],
],
],
'contactus' => [
'type' => Literal::class,
'options' => [
'route' => '/contactus',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'contactUs',
],
],
],
'payment' => [
'type' => Literal::class,
'options' => [
'route' => '/payment',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'payment',
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\CrudController::class => Controller\Factory\CrudControllerFactory::class,
Controller\RegistrationController::class => Controller\Factory\RegistrationControllerFactory::class,
],
],
'service_manager' => [
'factories' => [
Service\PicManager::class => Service\Factory\PicManagerFactory::class,
],
],
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Crud/Entity')
),
'orm_default' => array(
'drivers' => array(
'Crud\Entity' => 'application_entities',
),
),
)
),
'session_containers' => [
'UserRegistration'
],
// The following registers our custom view
// helper classes in view plugin manager.
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => [
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'crud/crud/index' => __DIR__ . '/../view/crud/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
],
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];
This is upload.phtml
<?php
$this->headTitle('Upload a New Image');
$form = $this->form;
//$form->setAttribute('action',$this->url('crud',['action' => 'add']));
//$form->get('submit')->setAttributes(['class'=>'btn btn-primary']);
$form->prepare();
?>
<h1>Upload a New Image</h1>
<p>
Please fill out the following form and press the <i>Upload</i> button.
</p>
<div class="row">
<div class="col-md-6">
<?= $this->form()->openTag($form); ?>
<div class="form-group">
<?= $this->formLabel($form->get('name')); ?>
<?= $this->formElement($form->get('name')); ?>
<?= $this->formElementErrors($form->get('name')); ?>
</div>
<div class="form-group">
<?= $this->formLabel($form->get('avatar')); ?>
<?= $this->formElement($form->get('avatar')); ?>
<?= $this->formElementErrors($form->get('avatar')); ?>
</div>
<?= $this->formElement($form->get('submit')); ?>
<?= $this->form()->closeTag(); ?>
</div>
</div>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Type</th>
<th scope="col">Image Id</th>
<th scope="col">Image Name</th>
<th scope="col">Image Avatar</th>
</tr>
</thead>
<tbody>
<?php
if (empty($pic))
{
$this->_isError = true;
}
else
{
$this->_pic = $pic;
}
return $this;
?>
<?php foreach($this->pic as $this->$pics):?>
<tr>
?>
<th><h3> <?= $this->escapeHtml($pics->getId()); ?></h3></th>
<td><?= $this->escapeHtml($pics->getImageName()); ?></td>
<td><?= $this->escapeHtml($pics->getImageAvatar()); ?></td>
<td></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
[this is my page link in browser][1]
[1]: https://i.stack.imgur.com/Pul1C.png
I have inserted 5 images in table but now i m trying to view the id,name of image in table format in browser.but not able to do it
i m not able to pass the data from controller to view page.
how to pass the id and name from controller to view in zend framework 3
如果需要更多代码我将帮助代码中的问题是什么?如何将数据库表colums值从控制器传递到zend框架3中的视图我们可以将数据从控制器传递到视图的方式是什么?如果需要更多的帮助肯定会有所帮助 . 所以请检查代码并协助我解决错误 .