I started with laravel few days ago and I'm facing this issue:
The NO
is never returned!
This is Controller
, do you have any idea why?
Class TestController extends BaseController {
public function __construct()
{
if (!Auth::check()) return 'NO';
}
public function test($id)
{
return $id;
}
}
The constructor is a method that is called whenever a new instance of a (PHP) class is instantiated. This is object oriented programming 101. The code you shared will apply the auth:instructor middleware on all controller actions (in that class) except the action called attendanceShowToStudent .
Open the command prompt or terminal based on the operating system you are using and type the following command to create controller using the Artisan CLI (Command Line Interface). Replace the <controller-name> with the name of your controller. This will create a plain constructor as we are passing the argument — plain.
A Controller is that which controls the behavior of a request. It handles the requests coming from the Routes. In Laravel, a controller is in the 'app/Http/Controllers' directory. All the controllers, that are to be created, should be in this directory.
Constructor Injection. The Laravel service container is used to resolve all Laravel controllers. As a result, you are able to type-hint any dependencies your controller may need in its constructor. The dependencies will automatically be resolved and injected into the controller instance.
The Laravel service container is used to resolve all Laravel controllers. As a result, you are able to type-hint any dependencies your controller may need in its constructor. The dependencies will automatically be resolved and injected into the controller instance. Step 1 − Add the following code to app/Http/routes.php file.
Step 1 − Create a controller called MyController by executing the following command. app/Http/Controllers/MyController.php file. app/Http/Controllers/MyController.php Step 3 − Add the following line of code in app/Http/routes.php file. Step 4 − We are now registering all the methods of MyController by registering a controller with resource.
function __construct () is a PHP magic function. It is a simple constructor function which is called when we create an object of that Class. It is used in laravel because laravel is a framework of PHP. What's the difference between Laravel model functions “create” and “insert”?
<?php
class BaseController extends Controller {
public function __construct()
{
// Closure as callback
$this->beforeFilter(function(){
if(!Auth::check()) {
return 'no';
}
});
// or register filter name
// $this->beforeFilter('auth');
//
// and place this to app/filters.php
// Route::filter('auth', function()
// {
// if(!Auth::check()) {
// return 'no';
// }
// });
}
public function index()
{
return "I'm at index";
}
}
For laravel 5.x:
public function __construct()
{
$this->middleware(function(){
if (!Auth::check()) return 'NO';
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With