I made a PHP Symfony project for learning purposes. Now i am stuck, so i have a Controller, which renders a View. But inside that Controller i want to access another Controller and make an object, because i need it's method.
So in short: How can i make a class/object inside another class in Symfony?
This is my code:
<?php
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;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use AppBundle\Entity\User;
use AppBundle\Controller\LoginController;
class HomeController extends Controller
{
/**
* @Route("/", name="home")
*/
public function renderIndexAction(Request $request)
{
$user = new User();
$form = $this->createFormBuilder($user)
->add('username', TextType::class, array('label' => 'username:'))
->add('password', PasswordType::class, array('label' => 'password:'))
->add('save', SubmitType::class, array('label' => 'login'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$user_username = $user->getUsername();
$user_password = $user->getPassword();
$loginController = new LoginController();
$user = $loginController->checkAction();
$session = $request->getSession();
$data = "test";
$session->set('form_data', $data);
return $this->redirectToRoute('addressbook');
}
return $this->render('home/index.html.twig', array('form' => $form->createView()));
}
}
So i want to use the LoginController inside my HomeController. But it gives me an error:
Call to a member function has() on null 500 Internal Server Error - FatalThrowableError
PS: Yes i know that my app is not safe, but i am still learning basic OOP. So it might be a weird way to call a LoginController like that. But it's for learning purposes.