I'm using Laravel web framework for a PHP project. I have a question about this framework, the documentation (which is poor), and the community from IRC doesn't helped me.
I want to create a controller which will be executed before any other controller. I want in this controller to pass some variables to view. How is this possible?
Thank you.
You can do such thing in the before filter, in routes.php (, for Laravel 4 its filters.php).
Or if you need to do it in controller then create a BaseController which should extend Controller, in Laravel 3 you should have this already, base.php. And all your controllers should extend this BaseController, in its __construct or in before filter do this for ease:
View::share('key', 'value');
And use it as $key in your views.
Note, View::share can be problematic sometimes.
Related
I find myself writing same code for my resourceful controllers. Is there is a way to extract the the functions index(), create(), update(), show(), edit() & destroy() into a generic file ? Also, if we do that, will it give me an option to override the those functions in the respective controller?
All you need is a crud generator package like nvd crud generator. It creates working templates for controller, model and views which you can edit yourself to suite your needs. This solution will save you the trouble of writing the same code again and again with the flexibilty of customizing each controller.
However, if you need to have a generaic controller which you can extend from, you can do that as well. You can create a controller ResourceController with artisan make command and then extend all other resource controllers from it.
I'm a novice developer working on an existing Laravel app. I'm looking through the project and I don't see any controllers constructed anywhere but the controllers all have constructor functions.
Is this a Laravel thing or am I just missing something? Is controller instantiation handled in the routes or something? If so, is it bad practice to call a controller constructor manually?(although I can't think of a case offhand where you would want to do this)
From the docs: "The Laravel service container is used to resolve all Laravel controllers."
And: I often create a base controller in my apps, therefore I also have constructor in my extended controllers. It could also be useful to inject dependencies "properly" like shown in this example:
https://laravel.com/docs/5.2/controllers#dependency-injection-and-controllers
I am new in laravel and I have good experience in Cakephp. In Cakephp we use helper for views only But I have seen my exiting code in laravel, We use helpers in controller also. Is it fine to use helper in controller or not?
If yes then Please let me know, What is the disadvantage or advantage of using helpers in controllers?
Please suggest me...
If you're talking about global Laravel helpers, then yes, you can use them everywhere in your app (controllers, models, view, middleware, routes.php etc), because these are global helpers.
You can also define your own helpers which will also be available globally.
I've been using Laravel for a long time, and I'm now writing a micro-project using Lumen.
I need to pass some variables to all views. In Laravel I can use the View::share() function in a middleware or in the controller's constructor, but in Lumen there is no View class, and it looks like all view functionality is simply View::make() alias.
Is there a way to share variables to all views?
For performance reasons, Lumen doesn't register facades and service providers the way Laravel does. While the Laravel facades are included with Lumen, only some are aliased (View not being one of them), and only if you uncomment the $app->withFacedes(); line in bootstrap/app.php (you can check the Laravel\Lumen\Application::withFacades method to see which ones). So in order to use other facades such as View, you either need to alias the facade class yourself:
// "bootstrap/app.php" is a good place to add this
class_alias('Illuminate\Support\Facades\View', 'View');
Or you can include it with use wherever needed:
use Illuminate\Support\Facades\View;
The correct way to share data with views in Lumen is:
app('view')->share(...);
Some of Laravel's functionality that is not explicitly described in Lumen documentation can be accessed with Lumen's app() helper function.
I'm coming From CodeIgniter to Laravel.
So, is a bad idea using automatic routes to all of controllers?
Route::controller(Controller::detect());
Should I use this instead creating routes in routes.php?
Yes this is bad.
Controller::detect() is actually not present in Laravel 4 because it is a bit broken.
detect() will go through your filesystem and return controller files, but this is a bad idea because the order you define your routes matters. If you have any nested controllers you will find this breaking very easily.
detect() will also return files in a different order depending on the file system, so this leads to a lot of unpredictability.
I would argue that you should define all your routes any ways, it is a lot easier to read and debug.
One of interesting things about Laravel that CI does not have is that for certain pages, you can route directly to the view without needing a controller at all. Think about static pages like 'About Us'. CodeIgniter would need you to set up a controller + view for that, even though the controller will do barely anything. In case of Laravel, you can route directly to a view in this case.
Setting up routes manually will allow you to set these short-circuited routes.
Automatic detection is a bad idea.
You can use routes or use Route::controller('mycontroller') or and array of controllers like Route::controller(array('mycontroller', mycontroller2');
Then you get the benefit, without the autodetect.
in laravel 4 :
you can use Restful Controller like documentation http://laravel.com/docs/controllers#restful-controllers
But
Route::controller() must take two parameters as minimum requirement
first parameter stands for URL respond to ,and second parameter is name of controller
also
you can write third parameter into Route::controller() is an array with names of actions (name of action with HTTP verb )and routes names for this actions
ex:
Route::controller('users','UsersController',array(
'getUsers' =>"listUsers" ,
));
route name for getUsers action is listUsers
Below is a good example to follow for CRUD and general purpose routing
type php arisan controller:make SampleController
edit routes.php and add
Route::resource('sample', 'SampleController');
Then type
php artisan routes to show the newly created routes