Symfony 4 and PhpSpreadsheet - php

Currently I try to use this PHP Spreadsheet Bundle for Symfony 4:
https://github.com/roromix/SpreadsheetBundle
But my attempts to use this bundle are not successful.
Can't find it as a service (php bin/console debug:autowiring)
and the way below is also wrong.
<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Roromix\Bundle\SpreadsheetBundle\RoromixSpreadsheetBundle;
class TranslationController extends Controller
{
/**
* #Route("/")
*/
public function index()
{
$RoromixSpreadsheetBundle = new RoromixSpreadsheetBundle();
//...
}
}
?>

I think for get service in your controller :
dump($this->get('phpspreadsheet'));
Because service name as phpspreadsheet : https://github.com/roromix/SpreadsheetBundle/blob/master/src/Resources/config/services.yml

Related

Entity class made with make:entity not recognized in Controller despite use namespace

In Symfony 4, using bin/console I've created new Entity:
php bin/console make:entity Being
...
It created Entity/Being.php and Repository\BeingRepository.php files.
Later I've also used make:migration and doctrine:migration:migrate, and my changes are now visible in database.
The problem is, when I wrote simple controller in Controller/DefaultController.php:
namespace App\Controller;
use App\Entity\Being;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* #Route("/", name="blog_index")
*/
public function index()
{
$entityManager = $this->getDoctrine()->getManager();
$beings = $entityManager->getRepository(Being::class);
return $this->render('default/index.html.twig',
["beings" => $beings->findAll()]);
}
}
I get error:
Class 'App\Entity\Being' does not exist
I also tried changing Being to explictly \App\Entity\Being with same result.
Note that I've not touched the files created by make:entity (Entity/Being.php and Repository\BeingRepository.php). First one looks like this:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\BeingRepository")
*/
class Being
{
...
Do I miss something?
Try just to execute composer dump-autoload or simply composer dumpa in console. In most cases it helps.
composer dumpa

Symfony Route not found

I'm really new with symfony, and I have strange problem.
I have a default controller, which looks like:
Both Controllers located:
src/AppBundle/Controller/
Names:
DefaultController.php
and
CmsController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* #Route("/")
*/
public function indexAction(Request $request)
{
die('Homepage');
}
}
And I'm trying to create new one:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class CmsController extends Controller
{
/**
* #Route("/cms")
*/
public function cmsAction(Request $request)
{
die('Cms Page');
}
}
Routing file looks like:
app:
resource: "#AppBundle/Controller/"
type: annotation
When I try to go for www.domainname.com - default controller shows "Homepage" - as it should.
When I try to go for www.domainname.com/cms - it gives error 404.
What can be the problem?
Probelm was simply with cache. Clearing it solved the problem.

Symfony2 - Creating a URL

I've just started using Symfony. I want to echo the $bookid when I call a URL like book/5 , but I stuck somewhere.
Here's my DefaultController.php file
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller {
/**
* #Route("/book/{id}", name="book")
*/
public function indexAction() {
return $this->render('default/index2.html.php');
}
}
file: /Myproject/app/Resources/views/default/index2.html.php
<?php
echo $id;
?>
When I call the book/6 , I get a blank page. What's missing? Do I need to change somewhere else, too?
You should declare that variable in your action and pass it to your view.
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
/**
* #Route("/book/{id}", name="book")
*/
public function indexAction($id)
{
return $this->render('default/index2.html.php', array(
'id' => $id
));
}
}
Whenever you have a parameter defined in your URL, you also need to "declare" it in your action function, so symfony maps it. Then, if you want to use it in your view, you have to pass it along.
If you are just starting with Symfony, I strongly recommend reading the Symfony Book and Cookbook. They are full of examples and relatively easy to understand, even for a newbie.
Other than that, the answer from smottt is correct. You add {id} in your route definition and receive it as parameter in your controller action.
Symfony book: Routing

Use elasticsearch in symfony2

I have installed elasticsearch using composer. This is my AppKernel.php file
new Elasticsearch\Client()
This is my TestController.php file.
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Elasticsearch\Client;
//use Elasticsearch\Common\AbstractFactory;
class TestController extends Controller{
/**
* #Route("/", name="test-homepage")
*/
public function indexAction(){
$client = new Elasticsearch\Client();
dump($client);
die;
}
}
I am using eclipse as my ide and it shows me error like elasticsearch\Client cannot be resolved.Why is this not working?
First of all: If you define use statement then you don't need FQCN. BTW your FQCN is not right it should starts with \ to prevent loading class from current namespace.
Then: in AppKernel.php you need to define bundles, not every library you installed.
If you have small experience in PHP try to use more easy-to-study frameworks. Symfony is mostly for experienced developers.
If you think that you can work with Symfony then I would recommend you bundle for integrating ElasticSearch and Elastica: https://github.com/FriendsOfSymfony/FOSElasticaBundle. It will save your time.
You should try to instanciate the imported class, using the short name (base on the use statements on your class), like this :
$client = new Client();
Or with the FQCN :
$client = new \Elasticsearch\Client();
(note that the new statement begin with a \ which prevent from trying to load the class from the current namespace (ie : AppBundle\Controller : \AppBundle\Controller\Elasticsearch\Client)

Class Not Exist when loading entity Symfony 2

I create a bundle and Entity inside the bundle. But when i try to get the entity or repository in controller using the entity manager it give an error that class not exist.
I try to debug using php app/console doctrine:mapping:info
it prompts every thing is correct. Output
Found 4 mapped entities:
[OK] Bitcoin\MyBundle\Entity\ProductCategory
[OK] Bitcoin\MyBundle\Entity\AdminUser
[OK] Bitcoin\MyBundle\Entity\Product
[OK] Bitcoin\MyBundle\Entity\User
My Controller code is as follows
<?php
namespace Bitcoin\MyBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Bitcoin\AdminBundle\Form\Login;
use Bitcoin\AdminBundle\Form\LoginValidate;
class LoginController extends Controller {
public function loginAction(Request $request) {
$user = $this->getDoctrine()->getRepository('User');
echo '<pre>';
prin_r(get_class_methods(get_class($user)));
die;
$pageData = array(
'name' => 'Login',
);
return $this->render('BitcoinAdminBundle:Login:login.html.twig', $pageData);
}
}
When i access this in browser the output is :-
i am previously worked with zend framework but new in symfony2. Any help will appreciate.
i find the issue and it is really a silly mistake.
For get an repository we should have to use the Bundle name with class name like this
$user = $this->getDoctrine()->getRepository('BitcoinMyBundle:User');

Categories