Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller class name vs file name #36

Closed
niallobrien opened this issue Jan 14, 2013 · 4 comments
Closed

Controller class name vs file name #36

niallobrien opened this issue Jan 14, 2013 · 4 comments

Comments

@niallobrien
Copy link

L4 it seems that your controller class cannot be called something like 'Auth.php' - must be 'AuthController.php' Class name must match file name, unlike L3 you could have 'Auth.php' class name 'Auth_Controller'.

This means when generating a URL, 'AuthController@login' must be used as opposed to 'auth@login' which was legal in L3.

@DPr00f
Copy link
Contributor

DPr00f commented Jan 14, 2013

I think L4 way is much more cleaner, and allows you to organize your app in a cleaner way, yes in L3 you could do 'auth@login', but you memorize that the first parameter is the Controller, in the L4 version you don't need to memorize anything, you just need to read and identify where is the controller and where is method.
I think L4 approach, to be more meaningful is good, you may write a little bit more of code, but you can read it as it was english.
Maybe they'll change it to L3 Routes, but i really doubt that.

@bencorlett
Copy link
Contributor

Much prefer L4's way of specifying controllers to respond a route. There is no convention anymore (which I'm stoked about) for naming of, well, any class in L4. This leads to huge flexibility.

If you require your app to have this logic, you could create a package and put it up on composer or you could simply edit a couple of files to manipulate the core to behave like you'd want. You could also do something like:

<?php

// in start.php
$app['router'] = $app->share(function($app) { return new MyCustomRouter($app) });

// in MyCustomRouter.php
class MyCustomRouter extends Illuminate\Routing\Router {

    protected function createControllerCallback($attribute)
    {
        list($controllerAlias, $method) = explode('@', $attribute);

        $actualController = ucfirst($controllerAlias).'Controller';

        return parent::createControllerCallback($actualController.'@'.$method);
    }

}

And vòila, you've got your app-specific controller assumptions and logic.

@niallobrien
Copy link
Author

I think I forgot to dump-autoload after renaming. Foo.PHP and classname Foo works fine.
However, a note to others, perhaps keeping Controller in the name is better. Yes, generating an URL isn't as clean (FooController@index) however, when you've lots of files open, it's a lot easier to know which tab is your controller! ;)

@robclancy
Copy link
Contributor

In L4 you can actually name things any way you like as it isn't mapped that way, it uses composers autoloader which will map a file path to whatever class is used. So you could name your file snake_case_name.php and then the controller RandomController_Name and it would still work fine.

joelharkes pushed a commit to joelharkes/framework_old that referenced this issue Mar 7, 2019
gonzalom pushed a commit to Hydrane/tmp-laravel-framework that referenced this issue Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants