Separate Laravel controller for each URL route? - laravel

I am building a REST API server (in Lumen, actually, rather than Laravel) with multiple endpoints that allow various operations to be performed on resources such as Users, Accounts, and Products. For example, here are the routes I have defined for the User resource:
GET /v1.0/user
POST /v1.0/user
GET /v1.0/user/{username}
PUT /v1.0/user/{username}
DELETE /v1.0/user/{username}
I currently have all of these API routes for a particular resource defined in a single controller. For example, here are my routes for the User resource:
$router->get('/v1.0/user', 'UserController#listAll');
$router->post('/v1.0/user', 'UserController#createUser');
$router->get('/v1.0/user/{username}', 'UserController#getUser');
$router->put('/v1.0/user/{username}', 'UserController#updateUser');
$router->delete('/v1.0/user/{username}', 'UserController#deleteUser');
Some of the controller logic is getting pretty complex, and I am now finding that my controller files are getting really, really long. I am now thinking that I should use a separate controller file for each route, to make the code more maintainable.
My question is whether there is any idiom or convention I should follow with regard to file/folder naming or structure. Should I create a subfolder under Controllers for each resource (ex: Controllers/User/UserCreateController.php)? Or is this entirely a matter of personal choice?

You should check out Single Action Controller which take only the __invoke() method and can handle one single route.
By the way, what I see usually is that when a controller logic is getting complex, is time to refactor and move that complexity outside the controller.

You do not need to create sub folders or multiple controllers. Use a single controller User Controller which only contains entry points of each route. Move the business logic outside of the controller by creating a class or group of classes that take care of the process.
For example : You can create another directory Libraries under app folder and create a class User under Libraries which contains all functions of User resource.
app/Libraries/User.php
namespace App\Libraries;
class User {}
Now, you can access this class and functions using the namespace within your User Controller
namespace App\Http\Controllers;
use App\Libraries\User;
class UserController extends Controller {}

Related

Call controller within another controller - CodeIgniter

I need to call a controller say 'faq_view' inside admin controller as the URL structure admin/faq_view like this how I can do this?
e.g:
site.com/maincontroller/function
site.com/maincontroller/othercontroller/function
Then, just redirect the page. Else if you want to just call the function, call it via AJAX.
It depends what you exactly want to do. If you want to just invoke the function, its not the right way. Controller as it defines itself controls the flow of the pages that comes on sequence. Controller is responsible to send commands to its associated view to change the view's presentation of the model.
So, if you are saying you want to call controller within another controller, that should mean you are about to redirect to another page.
Updated answer:
Just assume you have new_function on maincontroller that calls the function from othercontroller. The function does not need to be defined on othercontroller.
Add the following line on routes.php.
$routes['maincontroller/new_function'] = 'othercontroller/new_function';
Now, you can call the function of othercontroller as maincontroller/new_function.
You can always call a controller inside another controller, but this only works for calling one controller as far as I have tried. Let's say you are trying to load a controller inside a controller. You can try this:
$this->load->library('../controllers/myothercontroller');
Then do this:
$this->myothercontroller->function_name();
That's it! You can now access any function inside myothercontroller (controller) in your current controller. I hope this helps too.
Your controllers are part of the presentation layer and should not contain application logic. That means you should never need to call a controller from another controller, instead refactor your application and move the domain logic to the model layer.
Now if you have a method that you need in multiple controllers, say for example you need a template method that automatically adds your header and footer views.
If that is the case, create a base class that your controllers extend.
If you are talking about just a routing issue, then just use the routes file for that. I don't like the CI automatic routing and it should be avoided as it will result in duplicate URLs for the same resource.

Basic on Codeigniter controller

I am new in codeigniter framework.I have some question.When we write some controller class then we call constructor.I am loading some library and some helper.I want to know what is the perfect way for loading this helper and library under construct class or under other functions.If i load everything in construct class then what is the disadvantages for it?if i use more controller class for one project then it is good or bad.Like i want to use some controller class for ajax functionalities,some for form submission,some for other sector.My english is not so good.Please help me.
For common libraries and helpers used by all functions of your controller try to load it in constructor or autoload. Except that for specific libraries e.g. payment gateway load inside the function.
How you use or how many controllers you are using depends upon your needs. But I would suggest you to build separate controllers for separate functions. Like admin for admin functions, users for user related functions, and so on. It build a user friendly URL too.
Well! everything depends on your requirements.
If you need any library , model or helper globally you can autoload it autoload.php.
It means the loading will be done on each request.
if you need any library , model or helper throughout your controller methods you can load them in constructor.
It means loading will be done on each method of controller.
if you need any library , model or helper for specific method you can load them in method.
It means loading will be done in method only.
For example let suppose you nees session and database library throughout your application so you can autoload it.
For a specific controller you need priviliges library so load it in constructor.
For email you need to send load email library in function.
These are only examples. You can explore SO to find more.
Loading takes time and performance. Not noticeable time by human standards, but time regardless. In order to cut of some slack to your server in the future, you do it a favor by only loading whatever is it you require for each page/controller method.
Used (almost) always
You can use application/config/autoload.php for things you almost always use. I usually include the helper url here.
Used (almost) always inside specific controller
Load it inside controller __construct. Don't forget to include parent::__construct();.
Used inside specific page
Load it inside method/function.
Used inside specific function
Load it at beginning or inside if-statement in function. You can load it numerous of times, it will only execute first time.

How to prevent duplication of code

We're currently developing an E-commerce site. We have a public and admin module.
Sometimes we offer the same functionality in both modules like viewing of products or creating of orders. But there are also some functionalities that is present in either public or admin like adding of products (which is in admin).
Our problem is that common functionalities lead to duplication of logic. We need to implement it in both modules.
One way of solving the problem is to make use of layers. So what we did was move the common logic into the Model. However, the controller is still duplicating codes like the one shown below:
public function invoice() {
$this->Invoice->create();
$this->Invoice->setCustomer($this->getCurrentUser);
$invoice_items = // get list of items from post
$this->Invoice->setItems($invoice_items);
$this->Invoice->save();
}
My question is, is it wise to create a webservice that will encapsulate this logic and you just have to call it from the admin and public modules..
How does Magento implement the public and admin panels. And how does it manage its logic..
I would recommend you not to do that. From your question, it is not exactly clear what sort of 'logic' are you referring to, but it does not seem too complex from your example. In general, business logic should be coded within the Model, controller, or Helper portions of the code. It can even reside in a separate extension as long as you set dependencies properly in the main xml file of the extension.
It seems that you may benefit from placing your logic in a helper class. The default helper file resides under /app/code/community/company/extension-name/Helper/Data.php. Then you can call the helper method anywhere in the backend (Block, Module, or controllers) by using this piece of code:
Mage::helper('extension-name')->getLogic()
Or you can call the same helper method from the view (phtml files) like this:
$this->helper('extension-name')->getLogic()

codeigniter class name convention

When I'm developing in NetBeans I have a lot of confusion due to the amount of tabs open. In most cases the controller has the same name of the view or model.
In the convention style guide they say that you can prefix a controller file name with a custom suffix, but doesn't work.
My question is, there is any chance to end all the controller files with "_controller"?
In my example the class is class Verify_login extends CI_Controller { and the file is named verify_login.php. Tried with controller.verify_login.php like they say in the guideline but as I say, doesn't work. Lots of confusion in codeigniter's documentation.
Since the controller is the only thing exposed in the URL, I usually name my views and models with an indicator like "user_view" or "user_model". The controller would just be "user" and in this way I always know which file I'm working on.

How to set a custom URL path for a controller without creating new routes?

I wonder if there is attribute (built-in or some open source) for me to tag my controllers with the specific URL segment I want it to use, as in:
[MagicUrlRoute("status")]
public class InternalNameNotToBeRevealed : Controller
{
public ActionResult Show()
{
...
}
}
This way, instead of "/InternalNameNotToBeRevealed/Show" being what the user sees, it will be "/status/Show". This might be nit-picking, but it bothers that I have to use the controller class name as the official URL path.
Now, I do understand I could create a custom-route on global.asax, but that will be a lot of work for hundreds of controllers.
I found this very handy library to do exactly that, but only for actions:
http://maproutes.codeplex.com/releases/view/39888
I appreciate any suggestions.
You could have a listing of the mappings and just call MapRoute in a loop to register all the custom mappings. The mappings could be a dictionary, or you could even scan all your controllers once on App_Start, collect a custom attribute value and then use those to build the mappings. However, I'm not sure how well that would perform for a large number of mappings.
If you wanted a higher-performance mechanism, you'd have to create your own Route. You should be able to do this by inheriting from System.Web.Routing.RouteBase and overloading GetRouteData and GetVirtualPath to do the mapping. When constructing RouteData, you can just provide the existing System.Web.Mvc.MvcRouteHandler as the route handler, and as long as your route data contains 'controller' and 'action' values, it should continue down the MVC pipeline. Then just use the Add method on RouteCollection to add your route. You can take a look at MapRoute in System.Web.Mvc.RouteCollectionExtensions for some insight on how MVC adds it's route.

Resources