nova-framework / framework

Demo application using Nova - this is an extensive playground, use "app" repository for real applications.
http://novaframework.com/
MIT License
418 stars 210 forks source link

CRUD structure with SMVC #195

Closed josecarlosweb closed 8 years ago

josecarlosweb commented 8 years ago

Maybe this question is more for other types of matter. But I believe it interesting to discuss here. In my projects with SMVC generally, I have a controller for each entity. For simple CRUD, do as follows:

public function index(){
  // list of records here
   // I call a view to table, for example.
}

public function new(){
    // call a view to the form
}

public function saveNew(){
   // use the Model unless the data
}

public function edit($id){
   // I call a view to the form, passing the object
}

public function saveEdit($id){
    // use the Model unless the data
}

public function obliterate($id){
    // remove the item
}

My routes are something like this:

Router::get('client', 'Controllers\Client@index');
Router::get('client/remover/(:num)', 'Controllers\Client@obliterate');
Router::get('client/new', 'Controllers\Noticias@new');
Router::post('cient/new', 'Controllers\Noticias@saveNew');
Router::get('client/edit/(:num)', 'Controllers\Noticias@edit');
Router::post('client/edit/(:num)', 'Controllers\Noticias@saveEdit');

That's the structure I use always. At first it seems quiet, but when I have a project with 10 or more CRUD, starts to get boring / tedious.

Is correct to implement this way? Even if they are correct, what is the structure that more "save code"?

jimgwhit commented 8 years ago

It doesn't really matter for example I have one site where I show cats and dogs and I have separate controllers and models for each however I could have combined these controllers into one so it's kind of up to you if you want to combine some models and controllers or make separate models and controllers. I don't believe there's really a right or wrong answer in this case.

dcblogdev commented 8 years ago

nothing wrong with what your doing, but I get your point doing the same thing as a starting point for multiple controllers is tiresome.

I'm wondering what the best solution is, we could create a script that creates a desired structure but it would need to be ran on the server then removed afterwards a bit like an installer, I don't really like the sound of that.

Maybe a command line tool similar to Laravel's might be a better approach either way I don't have a ready made solution for this.

Looking at Syfony's console component could be used for this task http://symfony.com/doc/current/components/console/introduction.html given the choice I think I prefer command line of a php script designed to run from a browser.

josecarlosweb commented 8 years ago

@daveismyname I know these tools of Synfony, but I'm not very "fan" them. I thought I'd create something like a controller that extends \Core\Controller, and has these methods have given as an example. On the other hand, the Controller implementation would be identical to what is already performed. I thought here to analyze this tool http://www.grocerycrud.com/, and see the possibility of creating one for SMVC.

dcblogdev commented 8 years ago

building something like that is possible but would take time to build. It looks very good and very in depth.

If all you want is to create the controller files with set structures in place that can be done fairly easily with php, for instance I've written an installer for a CRM/CMS software built on top of SMVC that will create a config and htaccess file as well as creating the database tables.

using nothing more then a form and fopen commands to write data to a file. The site you linked to does a whole lot more then that.

jimgwhit commented 8 years ago

Actually if you have Visual Basic 6 you can write your own code to auto generate models and controllers and view code, I have done this in the past. Just the skeleton stuff not the detailed code. Started out as a hobby but got quite intriguing after a while programming this stuff in Visual Basic. I have even written special routines to all of the data that goes into the array for an update I must have about 25 to 30 special routines to automate this kind of stuff so I don't have to hand type it every time. I have always liked my own custom code not that auto-generated stuff like cake PHP spits out because you have to modify so much to get it to do what you needed to do I just soon write my own stuff and be done with it.
That's not to say that a little auto generator for a skeleton controller, model, and view wouldn't be good but not something like cake spits out.
But not part of the framework it would be nice to have another github repository for something like that so the framework doesn't get bogged down. La--- is like 17 meg ridiculous.

dcblogdev commented 8 years ago

I always write my controllers/models from scratch but I do like the idea of having a crud/wizard/generator once I have a module setup I use that as a blueprint for similar ones as not all of my modules use the same structure.

Now you've brought it up I'm keen to explorer the options and see what can be put together. As @jimgwhit has said third party programs can do this I think phpstorm can do that too.

jimgwhit commented 8 years ago

Like I said though have it as another repository not included in the framework don't forget we are trying to keep the framework simple and small but if downloaded separately and used in conjunction with the framework it would be great.

volter9 commented 8 years ago

Guys, I propose something like that:

Router::route can accept a callback function instead of controller@method format. You can callback to map URL to your REST action, something like that:

Router::any('api/(:any)/(:any)/(:all)', function ($controller, $action, $args) {
    $controller = "\Controllers\$controller";

    if (class_exists($controller)) {
        $controller = new $controller;
        $args       = explode('/', $args);

        call_user_func_array([$controller, $action], $args);
    }
    else {
        // Not found
    }
});
dcblogdev commented 8 years ago

@jimgwhit Yes new things like this would be an add-on stored outside of the framework. I'm looking at updating the website over the next month or so.

@volter9 I may be missing your point as the controller still needs to exists, are you proposing a more dynamic way to call the various methods of a controller?

volter9 commented 8 years ago

@daveismyname Yes, controller have to exists for current example, but nothing stopping you from creating a function/class/helper/whatever to make it even more dynamic.

are you proposing a more dynamic way to call the various methods of a controller?

For OP's current CRUD problem, yes I do. Instead of writing loop which populates similar routes (this way it's bloating routing procedure), or copy-pasting by hand, I propose more dynamic way of doing it. Maybe it will turn out to be helpful :)

jimgwhit commented 8 years ago

@volter9 I believe he is referring more of a generator to generate some of the tedious repeat code not so much a change in any kind of routing. A generator could also generate the routing code as well so it doesn't have to be hand typed each time. I strongly feel that someone should write their own generators as stated I've done this in Visual Basic 6 with no problem it's fun does take a little while.

volter9 commented 8 years ago

@jimgwhit my proposal was to avoid external add-on/wizard/generator or modifying core classes.

jimgwhit commented 8 years ago

Also I don't think we need to go back to the original jream style that didn't really have a router, I believe it was more of a front controller it did work good but we've come a long way. If this individual wants a front controller surely he could override what comes with the framework and implement his own rather than messing with the framework.

jimgwhit commented 8 years ago

@volter9 said

my proposal was to avoid external add-on/wizard/generator or modifying core classes

As Dave said things like this would be an add on stored outside of the framework.
My main point is if someone is advanced enough they can write their own generator code and if they choose to share it fine otherwise they have their own generator code like I do. Mine just happens to be in Visual Basic. When Dave starts that new section I will try to convert a couple things over to PHP and upload them to a repository.

geomorillo commented 8 years ago

Maybe Dave can make a new repository for addons so we can contribute there