I am trying to call middle-ware in constructor of my controller.
My PostController class is below
class PostController extends Controller
{
public function __construct()
{
$this->middleware( ['auth:admin', ['only'=> ['store', 'update']]], ['auth:client', ['only'=> ['index', 'view']]]);
}
}
Please suggestion or correct me if I am wrong.
I think the best way do it in routes
Route::post('path', 'IndexController#store')->middleware(['auth:admin']);
Route::get('path', 'IndexController#index')->middleware(['auth:client]);
Or in group, for example:
Route::group(['middleware' => ['auth:admin']], function ($route) {
$route->post('storePath', 'IndexController#store');
$route->put('updatePath', 'IndexController#update');
});
Yes, you can call the middleware function multiple times.
class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth:admin', ['only'=> ['store', 'update']])
$this->middleware('auth:client', ['only'=> ['index', 'view']]);
}
}
Related
I am trying to create a reusable Blog package. Here are my package routes:
Route::group(['middleware' => ['auth']], function () {
$path = 'MyPackages\Blog\Controllers';
Route::resources([
'blog' => "{$path}\BlogController",
'post' => "{$path}\PostController",
'comment' => "{$path}\CommentController",
'tag' => "{$path}\TagController"
]);
});
Which I am registering in my ServiceProvider boot()
$this->loadRoutesFrom(__DIR__.'/routes/web.php');
In my controller:
<?php namespace MyPackages\Blog\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use MyPackages\Blog\Models\Tag;
class TagController extends Controller
{
public function __construct()
{
parent::__construct();
}
public function index(Tag $tag)
{
$posts = $tag->posts;
return view('blog::post.index', compact('posts'));
}
}
My Parent __construct() looks like this:
public function __construct()
{
$this->middleware('auth');
}
But I keep getting redirected to the home page.
In my routes/web.php main routes file, I simply have a bunch of route definitions and a call to Auth::routes();
How can I stop it from redirecting me to the home page?
I recognize that the BlogController will need to be moved out of auth but everything else will have auth protection.
Any help?
Been searching hours for solution online but could not find a solution to this problem:
BadMethodCallException in RedirectResponse.php line 228:
Method [guest] does not exist on Redirect.
This is my controller:
class MemberController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('member.home');
}
}
class SessionController extends Controller
{
public function __construct()
{
$this->middleware('guest', ['except' => 'destroy']);
}
public function create()
{
return view('session.create');
}
}
This is my routes/web.php:
Route::get('/member', 'MemberController#index');
Route::get('/login', 'SessionController#create')->name('login');
When I try to access 127.0.0.1/member, the above error pops up.
Any idea?
you are setting your /member route to point to create method, which is not exists in your member controller object,
this line:
Route::get('/member', 'MemberController#create');
you may need to change it to :
Route::get('/member', 'MemberController#index');
OR
by changing your index method name in your member controller, or creating new method called create if you are using index method in another context:
public function index()
to be :
public function create()
I'm using Laravel 4.2
I have a ressourceful route like this:
Route::resource('customers', 'CustomersController');
How can I add a filter, let's say 'auth' filter to all corresponding routes and how to target only some of them, let's say I want only to filter access to the named route 'customers.create'.
You can define a filter in your Controller's constructor:
public function __construct()
{
$this->beforeFilter('auth', ['only' => ['update', 'store']]);
}
If you have many resources you can use route groups:
Route::group(['before'=>'auth'], function () {
Route::resource('customers', 'CustomersController');
// ... another resource ...
});
...and specify beforeFilter in each Controller's constructor.
OR:
Use a simple if statement in routes.php:
if (Auth::check()) {
Route::resource('customers', 'CustomersController');
} else {
Route::resource('customers', 'CustomersController', ['except' => ['update', 'store']]);
}
Create a base controller for resources that use the same filter and extend it:
class AuthorizedController extends BaseController {
// ... constructor with beforeFilter definition ...
}
class CustomersController extends AuthorizedController { ... }
Let's say I have a UsersController. In that controller there is a handler for website.com/users/login and website.com/users/register
How would I handle a route of website.com/users within the controller similarly how I would with the other handlers?
In routes.php:
Route::controller('users', 'UserController');
In UserController.php:
class UserController extends BaseController {
public function getIndex()
{
# GET website.com/users
}
public function getLogin()
{
# GET website.com/users/login
}
public function getRegister()
{
# GET website.com/users/register
}
}
The Laravel docs have more examples: http://four.laravel.com/docs/routing
I have this route: Route::controller('/', 'PearsController'); Is it possible in Laravel to get the PearsController to load a method from another controller so the URL doesn't change?
For example:
// route:
Route::controller('/', 'PearsController');
// controllers
class PearsController extends BaseController {
public function getAbc() {
// How do I load ApplesController#getSomething so I can split up
// my methods without changing the url? (retains domain.com/abc)
}
}
class ApplesController extends BaseController {
public function getSomething() {
echo 'It works!'
}
}
You can use (L3 only)
Controller::call('ApplesController#getSomething');
In L4 you can use
$request = Request::create('/apples', 'GET', array());
return Route::dispatch($request)->getContent();
In this case, you have to define a route for ApplesController, something like this
Route::get('/apples', 'ApplesController#getSomething'); // in routes.php
In the array() you can pass arguments if required.
( by neto in Call a controller in Laravel 4 )
Use IoC...
App::make($controller)->{$action}();
Eg:
App::make('HomeController')->getIndex();
and you may also give params
App::make('HomeController')->getIndex($params);
You should not. In MVC, controllers should not 'talk' to each other, if they have to share 'data' they should do it using a model, wich is the type of class responsible for data sharing in your app. Look:
// route:
Route::controller('/', 'PearsController');
// controllers
class PearsController extends BaseController {
public function getAbc()
{
$something = new MySomethingModel;
$this->commonFunction();
echo $something->getSomething();
}
}
class ApplesController extends BaseController {
public function showSomething()
{
$something = new MySomethingModel;
$this->commonFunction();
echo $something->getSomething();
}
}
class MySomethingModel {
public function getSomething()
{
return 'It works!';
}
}
EDIT
What you can do instead is to use BaseController to create common functions to be shared by all your controllers. Take a look at commonFunction in BaseController and how it's used in the two controllers.
abstract class BaseController extends Controller {
public function commonFunction()
{
// will do common things
}
}
class PearsController extends BaseController {
public function getAbc()
{
return $this->commonFunction();
}
}
class ApplesController extends BaseController {
public function showSomething()
{
return $this->commonFunction();
}
}
if you were in AbcdController and trying to access method public function test() which exists in OtherController you could just do:
$getTests = (new OtherController)->test();
This should work in L5.1