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
Related
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']]);
}
}
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 want to use middleware in Usercontroller on only getDashboard() and getUserlist() method, but it didn't work for me.
also I add in controller constructor.
My Controller:
class Usercontroller extends Controller {
public function __construct() {
$this->middleware('auth',['only' => ['getDashboard']]);
//$this->middleware('auth');
}
##This method render home view.
public function index() {
// dd('yahoo');
return view('welcome');
}
##To add new user.
public function getAdduser() {
return view('register');
}
#To render dashboard view.
public function getDashboard() {
return view('dashboard');
}
When I directly type in url http://localhost/rabble/index.php/user/dashboard.it still display display without authenticate user is login or not.
I was wondering can I change default view folder for a controller in Yii2?
If we can change layout just by using public $layout, how we can do it with view?
Class HomeController extends \yii\web\Controller
{
public $layout = 'mylayout';
public $view = 'newview';
public function actionIndex()
{
return $this->render('index');
}
}
To achieve that your controller should implement ViewContextInterface.
use yii\base\ViewContextInterface;
use yii\web\Controller;
class HomeController extends Controller implements ViewContextInterface
Then just add getViewPath() method which should return the desired directory path:
public function getViewPath()
{
return Yii::getAlias('#frontend/views/newview');
}
You can use aliases here.
Also check the official documentation about organizing views.
Since 2.0.7 you can simply write in your controller's init() method: $this->viewPath = '#app/yourViewPath'
I'm on Yii 2.0.42.1, And added this in my controller.
public function init()
{
$this->viewPath = '#app/modules/report/views/test';
parent::init();
}
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