So, I'm adding the Gate facade to my constructor in my UserController
public function __construct()
{
if (Gate::denies('manage-user')) {
abort(404);
}
}
Everything works as expected but one thing, now php artisan route:list show following error
$ php artisan route:list
[Symfony\Component\HttpKernel\Exception\NotFoundHttpException]
If I remove the Gate, php artisan route:list run fine. Anyone know why this is happening? And how to solve it? Can artisan bypass the Gate facade?
I think you want to avoid doing checks like this in a controller constructor. The Laravel docs show a number of ways to implement the authorization checks, none of them are in the controller constructor.
https://laravel.com/docs/5.2/authorization#checking-abilities
I would create a FormRequest personally, with an authorize method that does the check. Then you inject that FormRequest into each method, and it runs authorize automatically.
https://laravel.com/docs/5.2/authorization#within-form-requests
https://laravel.com/docs/5.2/validation#form-request-validation
I Used this command
public function __construct()
{
// check if request not from cli
if ('cli' != php_sapi_name()) {
$this->authorize('is_admin');
}
}
Related
I am using laravel to have access to my database. I have used the command php artisan make:controller CategoriesController --resource in the terminal to create a class where I can access different methods in one route.
The routing code for the class is: Route::apiResource("categories", CategoriesController::class);. With /categories I can get to the index() method (via get), where I can show my table values. But I do not know how I can use other methods. For example I have created test() with a simple return ["Result"=>"Working"].
I have tried /categories/test /categoriestest /categories%test but I can not show the result from test().
Simple routing works fine when I use specific routes for every method, but I want to make a more clear code so I would like to use the --resource to only have one route.
I have created the routes and views needed for authentication using one simple command:
php artisan make:auth
Everything works fine login and register sections. However when I go to my controller's constructor to check if the user its logged in I always get false response; even though the user its logged in!
public function __construct()
{
dd(Auth::check());
}
Any idea?! And yes I did use Auth; at the top.
Middleware (and therefore setting the logged in user) don't happen until after the controller constructor. See this related question/answer for more details:
Laravel 5 Auth is non object in Controller construct
Use the helper function auth()->check() and add
$this->middleware('auth') to the function __construct() method.
I am using Laravel 5.2 and facing strange behavior at local and server.
The below code working fine in Local where as not working in server.
In controller newly added method is not working, even updating the existing method also not working.
Routing code
routes\web.php code as below
Route::get('dbimport/','DbImportController#index');
Route::get('dbimport/test','DbImportController#test');
DbImportController code
app\Http\Controllers\DbImportController.php as below
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use \Illuminate\Database\Connection;
use \Illuminate\Http\Request;
use App\Http\Requests;
class DbImportController extends Controller
{
public function index() {
return view('dbimport');
}
public function test() {
return 'This is a test method';
}
}
The above code is working fine and the methods also working fine, but today I added new method which is called csv and updated test method content.
Updated code as follows
class DbImportController extends Controller
{
public function index() {
return view('dbimport');
}
public function test() {
return 'This is a test method modified # 27/10/2016';
}
public function csv() {
return view('csvimport');
}
}
And routes/web.php
Route::get('dbimport/','DbImportController#index');
Route::get('dbimport/test','DbImportController#test');
Route::get('dbimport/csv','DbImportController#csv');
Now, if I run the test method it's showing old content as "This is a test method" not showing updated code.
If I run the new method dbimport/csv it's showing an error as below
I run all the following cache clear commans,
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
But no use.
Please help me, thanks in advance.
Try updating routes/web.php to:
Route::get('dbimport/csv','DbImportController#csv');
Route::get('dbimport/test','DbImportController#test');
Route::get('dbimport/','DbImportController#index');
I am newbie to laravel.
I created new controller - book.
This is my code -
class BookController extends BaseController {
public function index()
{
return View::make('book.index');
}
public function insert()
{
return View::make('book.insert');
}
}
My routes.php -
Route::get('book/', 'BookController#index');
//Route::any('book/insert', array('uses' => 'BookController#insert'));
When i uncomment 2nd line, i can access insert page.
Is it possible to access pages without add them to routes.
Now it produce this error
You may read about resource controller.
Execute this on terminal:
php artisan make:controller BookController
This command will generate BookController.php in your app/controllers folder. Read the code for more information.
Define in your routes/web.php file:
Route::resource('book', 'BookController');
Actions Handled By Resource Controller:
Route filters provide a convenient way of limiting access to a given route, which is useful for creating areas of your site which require authentication. So it is better to use route.php as Laravel framework indicates.
you can add filters to there as well, refer documentation
Is there a way to hook into an artisan command easily? What I'm looking to accomplish is have a piece of code executed everytime the php artisan migrate command is executed.
Just add a listener some place in your code (even at the top of app/start/artisan.php) to listen for the 'artisan.start' event.
Event::listen('artisan.start', function($app)
{
// $app is instance of Illuminate\Console\Application
// since the $app hasn't figured the command yet you'll
// have to do it yourself to check if it's the migrate command
});
You may try something like this (you may put it in the filters.php file):
Event::listen('artisan.start', function($app) {
if( isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'migrate' ) {
// do something because it's migrate command
}
});
You can extend the Schema Builder facade and override the build function like so:
protected function build(Blueprint $blueprint)
{
your_function();
Parent::build($blueprint);
}
However, your function won't be called when using the --pretend option for the migrate. I don't know of any built-in ways to hook into the migration without extending either the Schema Builder or Migrator classes.