Using a class as a "model" in symfony

Hello,

In symfony3, I’m trying to use a class as a “model” (to match the MVC standard). I know that symfony is not a MVC framework, but still…

What I’ve done is to create a Model directory “/src/AppBundle/Model” and put my class file “imagingClass.php” into it.

I thought it would do the job, but symfony claims that :

Class AppBundle\Model\HomeController does not exist in /mnt/400Go/www/sy1/src/AppBundle/Controller/ (which is being imported from “/mnt/400Go/www/sy1/app/config/routing.yml”).

I can’t figure out what it means. Does symfony sees it as some kind of controller ? Should I set the Model directory elsewhere ?

Best regards,

MC

Post your routing file. The error message does not make much sense.

By the way, I suspect a bunch of other people may jump in here but the M in MVC usually does not imply a single model class. It’s a fairly vague letter (as opposed to V and C) and really refers to your actual domain data which might include a bunch of classes.

Here’s my routing.yml

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

homepage:
    path: /
    defaults:  { _controller: AppBundle:Home:home }
homepage:
    path: /home
    defaults:  { _controller: AppBundle:Home:home }

other_pages:
    path:      /{slug}
    defaults:  { _controller: AppBundle:Others:show }

I can’t see how it relates to the Model directory… Puzzling.

That implies you probably have the namespace wrong in your controller file. Please post the top portion of both AppBundle/Model/imageClass.php and AppBundle/Controller/HomeController.

If you happen to have your code in a repository then just post a link to it.

And of course make sure you have cleared your cache though that should not make a difference.

Here’s the controller :

<?php
// src/AppBundle/Controller/HomeController.php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class HomeController extends Controller
	{
    public function homeAction()
[...]

I’ve not specified any namespace in AppBundle/Model/imageClass.php

<?php
class imaging
	{
[...]

I guess I’ve made some noob’s error…

And try to follow Symfony conventions as much as possible. Classes should start with an uppercase letter. And the file name needs to match the class name exactly unless you have adjusted the autoloader.

// AppBundle/Model/Imaging.php
namespace AppBundle\Model;
class Imaging { ...

I assume your are using it in your controller so:

//AppBundle/Controller/Home.php
...
use AppBundle\Model\Imaging;
...
public function homeAction() {
    $imaging = new Imaging();

Ok, I bow my head, fall on my knees. It works. I’m a noob.
Thanks a lot. Symfony is tricky and beautiful.

Symfony IS a MVC framework.

There’s a great source of knowledge about working with a datebase by using Doctrine http://symfony.com/doc/current/doctrine.html. Symfony has a really nice documentation.

@ahundiak
Classes which we use to working with Doctrine should be stored in Entity directory and we call them Entities. In Symfony Model directory is intended to store Mapped Superclasses. Entities extends Mapped Superclasses. It’s common usage when you create standalone bundle.

If he had used Model directory instead Entity he wouldn’t be able to use e.g. php bin/console doctrine:schema:update --force

I apologise for possible mistakes. English isn’t my native language :/.

Well okay but it’s such an over used and over loaded term that it’s basically meaningless. Symfony bills itself as Request/Response framework. Request comes in, something hopefully useful happens then a response goes out. There are a few spots in the documentation where the term MVC pops up but I think those are just editorial oversights.

Read the question carefully. Nothing in there about Doctrine. Yes, Entity is a default directory for Doctrine but it’s easy enough to use any directory or directories that you want. And of course you don’t even need to use Doctrine. Keep in mind that this was a newbie type question. No need to confuse him with irrelevant details.

I doubt if you will find anything in the Symfony docs about this. Certainly nothing in the best practices document. Yes, some bundles do have classes there which are intended to be extended in a Model directory but again it has no relevance to this question.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.