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');
Related
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
I created src/Controller file named as sampleController.php:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class sampleController extends Controller
{
/**
* #Route("/hello")
*/
public function number()
{
$number = mt_rand(0, 100);
return $this->render('sample/number.html.twig', array(
'number' => $number,
));
}
}
My twig file is this:
<h1>Number is: {{ number }}</h1>
And routes.yaml is this:
sample_asd:
path: /hello
controller: App\Controller\sampleController::number
I've installed annotations and --dev profiler. But when I navigate to http://localhost:8000/hello it gives:
HTTP 500 Internal Server Error
[Semantical Error] The annotation "#Route" in method App\Controller\sampleController::number() was never imported. Did you maybe forget to add a "use" statement for this annotation? in C:\xampp\htdocs\Projects\symfony_learning\config/routes../../src/Controller/ (which is being imported from "C:\xampp\htdocs\Projects\symfony_learning\config/routes/annotations.yaml"). Make sure annotations are installed and enabled.
I didn't understand what is the problem.
Thanks
You did forget to include annotation class. Try to add this.
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Update
For symfony 4 you should use this path.
use Symfony\Component\Routing\Annotation\Route;
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
I am getting a Class 'App\Models\User' not found error when I try too use the User class inside a controller class method. I have looked everywhere and tried everything with no luck! Here's what I've tried:
Check that class exists and is in the right path (it works everywhere else)
Add use App\Models\User; to the top of the controller file and just use User
Tried: new \App\Models\User
Run: composer dump-autoload
Run: php artisan dump-autoload
Run: php artisan clear-compiled
When I do dd(class_exists('App\Models\User')), I get \vendor\laravel\framework\src\Illuminate\Support\helpers.php:513:boolean false which confirms that the class really isn't accessible for some reason.
Any ideas?
EDIT
You will find questions similar to this but not the same. Please read question carefully. I didn't say the controller class was missing. I said a model class (User) was not accessible from inside a particular controller class. And that the model class works everywhere else.
EDIT: Code Excerpt
<?php
use App\Models\User;
use App\Models\Role;
use App\Models\Advert;
use App\Models\AdvertPhoto;
use App\Models\AdvertMetum;
use App\Models\AdvertMetaDatum;
use App\Models\AdvertMetaCategory;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Request;
class AdvertsController extends BaseController {
/**
* Show the form for creating a new resource.
* GET /adverts/create
*
*/
public function create()
{
// New user instance
// dd(class_exists('\App\Models\User')); // Outputs FALSE
$userx = new User; // Throws an Exception
return View::make('adverts.create');
}
}
I have managed to resolve this on my own. It turns out you have to tell Laravel what class and table will be used for authentication (a.k.a your 'User' class). I didn't know this (plus this is an inherited project). Apparently the User class was defined in the root namespace (i.e. \User) and Laravel was configured to look for \User. But sometimes I see \App\Models\User in the code and it gave me the impression that User was under the same namespace as the other models since they were ALL in the app/models/ folder. I have corrected this problem in config/auth.php by changing:
'model' => 'User'
to:
'model' => App\Models\User::class
And adding namespace at the top of app/models/user.php:
namespace App\Models;
Finally I set an alias in config/app.php like this:
'User' => 'App\Models\User'
So that where ever I've been using User::blah will not break (forcing me to add use App\Models\User; everywhere!)
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)