How Laravel requests behave about controller files compilation - laravel

In Laravel, when a request mapped to a specific controller method is made, does Laravel access/compiles all other controller files or just the file from involved controller?

Most of the classes you create in a Laravel application are loaded as needed. This includes Models, Controllers, Repositories... pretty much everything.
Autoloading is achieved using the PSR-4 spec. If you have a namespaced class like...
<?php namespace \Foo\Bar;
class Baz {}
Then it should live in...
app/Foo/Bar/Baz.php
When this class is used for the first time, the framework will attempt to load the class from that location.

Related

Create controller with already included classes laravel

I am working with Laravel framework and I want to include my common classes in the controller file while I am creating controller from php artisan. For now it creates controller like
<?php namespace ReaPro\Http\Controllers\Home;
use ReaPro\Http\Requests;
use ReaPro\Http\Controllers\Controller;
class HomeController extends Controller {}
I want to include my common classes like
<?php namespace ReaPro\Http\Controllers\Home;
use ReaPro\Http\Requests;
use ReaPro\Http\Controllers\Controller;
use ReaPro\Model\Page;
use ReaPro\Helpers\Common;
class HomeController extends Controller {}
So how can I pre include my
use ReaPro\Model\Page;
use ReaPro\Helpers\Common;
Classes somewhere so that these may come with each controller by default when I create controller with php artisan ?
The Laravel GeneratorCommand class is quite easy to extend for your own needs. Here is the ControllerMakeCommand which extends GeneratorCommand. The most important part is in the getStub() method, from here you can see that it returns either controller.stub or controller.plain.stub depending on the options. These stubs are here.
In short:
Create your own command for generating controllers
Extend Illuminate\Console\GeneratorCommand
Implement your own command in the same way as Illuminate\Routing\Console\ControllerMakeCommand
Create your own stubs

how to load module model class in CI main controller

I am using HMVC strucutre in Code Igniter, where I need to load module model class insider base CI controller or helper how can I do this.
i.e: load faq model in side view_helper
folder strucutre
Thanks
you can load a model in your helper the same way as you can load it in your controller:
$this->load->model('Model_name');
Once loaded, you will access your model functions using an object with the same name as your class:
$this->load->model('Model_name');
$this->Model_name->function();

ASP.NET Web API Help Pages Omit Controllers That Inherit From A Base Controller

I have a controller that I want to generate documentation for using ASP.NET Web API Help Pages.
When I directly inherit from ApiController the documentation appears:
public class ExampleController : ApiController
But when I inherit from a base controller, it is omitted:
public class ExampleController : ApiBaseController
...
public class ApiBaseController: ApiController
I have switched to delegation rather than inheritance, but I wanted to know how to make it work with inheritance.
Here is a tip I picked up in my experimentation.
The documentation leans heavily on the routes in your API config. If your controller isn't covered by a route, it won't show up. Additionally, the order of the routes in your API config is the order of the operations in your documentation.
To cover both of these points I have created named routes for each controller. This has the added benefit of making each route specific, rather than a single route with lots of optional bits. This ensures all my operations appear in the documentation, in a good order.
I have also added the API tester so the API can be called directly from the documentation.
Check the permissions in your base class. I had the same issue and is was a result of methods that should have been set as internal being protected.
Make sure that all your methods that need to be accessed by the parent item are set to internal and any methods that override the ApiController are set to protected.
Post your code if it still doesn't work.
Works like Gravy :)

How to layout base class and 3 extended classes in CodeIgniter?

This is the layout I'd like to use for controllers in CodeIgniter:
(base) editor
(extend) design
(extend) content
(extend) ...php
...where editor is a base class and design, content, etc. controllers extend editor. The editor class will have methods that are publicly accessible from the extended classes URL segments.
I've read some topics on here, and they recommended:
Library - this won't work as methods won't be publicly accessible (am I correct?)
Put base class in the same file as extended class and name the controller that - this won't work since I need to extend from multiple places.
Put all the files in the controllers folder, add require statements to each extended class - is this bad form?
I'm new to CI. What's the proper/correct way to handle this?
Thanks!
http://www.ellislab.com/codeigniter/user-guide/general/core_classes.html
extend the core CI controller with MY_Controller, then extend MY_Controller with your other controllers. all the other retain the functionality in MY and MY retains the functionality of the base controller
There is also this article which allows more controllers that don't have the MY_ prefix, which I use and find VERY useful!
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-base-Classes-Keeping-it-DRY

Regarding Symfony2 Session

In symfony2 every user created controller extends Controller Class as shown below,
class MyController extends Controller {
thus functions related to session handling are available with $this object, But controllers in Vendor and Core don't extend Controller class thus don't provide access to session related functions. So is there any way to use these functions without extending Controller class.
Presently I am using $_SESSION[], for setting and getting session variables.
Is there any way other than above.
Symfony2 provides a service for sessions, this is what you're trying to retrieve. All services in symfony2 are retrieved using the service container, which is what you're referring to with
$this->get('session');
To properly make use of the service container in your own controllers you can either...
Configure your controllers as services (see: here)
Extend the base Controller class provided by the Symfony2 stack (making the get() method available to your child Controller)
The first option is the correct way to go, you have full control over what services are then injected into your respective controllers (see service container documentation)

Resources