Simple question but i'm quite new to Laravel.
I've got my middleware function attached to my dish controller at the top, however i want it attached to my dish/create route specifically.
function __construct() {
$this->middleware('auth:restaurantuser');
}
I thought in theory this would work, but it doesn't.
public function create()
{
function __construct() {
$this->middleware('auth:restaurantuser');
}
return view('dishes.create')->with('restaurants', Restaurant::all());
}
Add the middleware in your specific route.
Route::get('dishes/create', 'DishesController#create')->name('dishes.create')->middleware('auth:restaurantuser');
Middleware for Route In Web file:
Route::get('dishes/create', 'DishesController#create')->name('dishes.create')->middleware('auth:restaurantuser');
Remove your middleware from constructor..
public function create()
{
return view('dishes.create')->with('restaurants', Restaurant::all());
}
for more detail go to this link
https://laravel.com/docs/6.x/middleware#assigning-middleware-to-routes
Related
I'm trying to set up a basic Laravel CRUD application, and I'm getting stuck setting up the pages for each action.
When I visit the route case/create, it opens the page for show instead.
routes/web.php
use App\Http\Controllers\HospitalCase as HospitalCase;
Route::controller(HospitalCase::class)->group(function() {
Route::get('/cases','index')->middleware('auth')->name('cases');
Route::get('/case/{id}','show')->middleware('auth');
Route::get('/case/create','create')->middleware('auth');
Route::post('/case/create','store')->middleware('auth');
Route::get('/case/edit/{$id}','edit')->middleware('auth');
Route::post('/case/edit/{$id}','update')->middleware('auth');
Route::get('/case/delete/{$id}','destroy')->middleware('auth');
});
HospitalCase.php controller
class HospitalCase extends Controller
{
function index()
{
echo 'index';
}
function create()
{
echo 'create';
}
function show($id)
{
echo 'show';
}
function store()
{
// validation rules
}
function edit($id)
{
return view('case/edit');
}
function update($id)
{
}
function destroy($id)
{
}
}
This is what I see on the browser:
I have been trying to figure this out for hours and can't think of what I'm doing wrong.
PS: The auth middleware is using laravel breeze (unmodified)
The reason it's showing the show route is because you defined
Route::get('/case/{id}','show')->middleware('auth');
before it, therefore, it's matching case/create as show('create')
Try defining the route afterwards.
Route::get('/case/create','create')->middleware('auth');
Route::post('/case/create','store')->middleware('auth');
Route::get('/case/{id}','show')->middleware('auth');
Just want to reiterate what #TimLewis has suggested, I think you need to put this route:
Route::get('/case/create','create')->middleware('auth');
Above this route:
Route::get('/case/{id}','show')->middleware('auth');
But you could try using Laravel’s route resource so you don’t need to write out all the routes -
use App\Http\Controllers\HospitalCaseController;
Route::resource('case', HospitalCaseController::class);
I created a small site by laravel 6, with the four blade index, create, edit, show and an authentication system, I want everyone to see the blades index and show, and the blades create and edit prohibit that if user authenticate.
TinghirsController.php
public function __construct() {
$this->middleware('auth');
}
public function index()
{
$tinghirs=Tinghir::orderBy('created_at','desc')->paginate(30);
return view('tinghirs.index', ['tinghirs' => $tinghirs]);
}
public function create()
{
return view('tinghirs.create');
}
public function show($id){
$tinghirs = Tinghir::where('id',$id)->firstOrfail();
return view('tinghirs.show', ['tinghirs' => $tinghirs]);
}
public function edit($id) {
$tinghir = Tinghir::find($id);
return view('tinghirs.edit', ['tinghir' => $tinghir]);
}
Route/web.php
Route::resource('tinghirs','TinghirsController');
As per the documentation, you can specify which controller methods you want to apply a piece of middleware to. In you're case you want to apply the auth middleware to all methods except index and show.
To achieve change the middleware call in your __constructor method to be:
$this->middleware('auth')->except('index', 'show');
Why is it that whenever I redirect something through the constructor of my Codeigniter 4 controller is not working?
<?php namespace App\Controllers\Web\Auth;
class Register extends \App\Controllers\BaseController
{
function __construct()
{
if(session('username')){
return redirect()->to('/dashboard');
}
}
public function index()
{
// return view('welcome_message');
}
}
But if I put it inside index it's working as expected.
public function index()
{
if(session('username')){
return redirect()->to('/dashboard');
}
}
The thing is, I do not want to use it directly inside index because I it need on the other method of the same file.
As per the Codeigniter forum, you can no longer use the redirect method in the constructor to redirect to any of the controllers.
Please refer the below link for more information
https://forum.codeigniter.com/thread-74537.html
It clearly states that redirect() will return a class instance instead of setting a header and you cannot return an instance of another class while instantiating a different class in PHP.
So that's why you can't use redirect method in constructor.
Instead, what I can suggest to you is that use the header method and redirect to your desired controller.
<?php namespace App\Controllers\Web\Auth;
class Register extends \App\Controllers\BaseController
{
function __construct()
{
if(session('username')){
header('Location: /dashboard');
}
}
}
If that's not feasible or difficult to achieve you can follow the below code
<?php namespace App\Controllers\Web\Auth;
class Register extends \App\Controllers\BaseController
{
function __construct()
{
//call to session exists method
$this->is_session_available();
}
private function is_session_available(){
if(session('username')){
return redirect()->to('/dashboard');
}else{
return redirect()->to('/login');
}
}
}
The 2nd solution will be more interactive than the first one. And make sure the method is private. So that it should not be called from other class instances.
The community team has also given a solution to look into the controller filter.
https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html
Please refer to the thread. I hope it may help you in finding a better solution.
In this case you shouldn't even be doing this kind of logic in your controllers. This should be done in a filter and not your controllers.
So you have your controller Register.
You should create a filter in your app/filters folder something like checkLogin.php
That filter should have the following structure:
<?php
namespace App\Filters;
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
class CheckLogin implements FilterInterface
{
/**
* Check loggedIn to redirect page
*/
public function before(RequestInterface $request, $arguments = null)
{
$session = \Config\Services::session();
if (session('username')) {
return redirect()->to('/dashboard');
}
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// Do something here
}
}
Then in your app/config/Filters.php your should add the filter to the desired controller.
public $aliases = [
'csrf' => \CodeIgniter\Filters\CSRF::class,
'toolbar' => \CodeIgniter\Filters\DebugToolbar::class,
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
'checkLogin' => \App\Filters\CheckLogin::class,
];
// List filter aliases and any before/after uri patterns
public $filters = [
'checkLogin' => ['before' => ['Register']],
];
For more information on filters and how to use then please check the documentation.
https://codeigniter.com/user_guide/incoming/filters.html?highlight=filters
You can then even create filters to your other controllers that would redirect to this one in case the user is not logged in.
Codeigniter 4 using initController() to create constructor.
You can't use redirect() inside __construct() or initController() function.
But you can use $response parameter or $this->response to call redirect in initController() before call another function in controller;
<?php namespace App\Controllers\Web\Auth;
class Register extends \App\Controllers\BaseController
{
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
if(session('username')){
$response->redirect(base_url('dashboard')); // or use $this->response->redirect(base_url('dashboard'));
}
}
public function index()
{
// return view('welcome_message');
}
}
I just got into CI 4 and i had the same issue as you did, since i've been approaching the login system as with CI 3.
So here is the proper way of doing it right. Enjoy.
Codeigniter 4 do not has any return on Constructor, but you can use like this
public function __construct()
{
if (!session()->has('user_id')) {
header('location:/home');
exit();
}
}
Don't forget to use exit()
But, you better use Filters
I'm doing an existence check within a middleware, by checking a route-parameter.
If the check succeeds, I'm attaching it's model to the request to make it available throughout the rest of the request-cycle, application.
// App\Http\Middleware\CheckForExistence.php:
...
public function handle($request, Closure $next)
{
// some checks...
// success
$request->attributes->add([
'company' => $someModel
]);
}
I now have a controller which 'needs' this information in a couple of methods. So my thought was to add it to the construct of the controller and add it as a protected var in the whole controller:
// App\Http\Controllers\MyController.php
<?php
use Illuminate\Http\Request;
class MyController extends Controller
{
protected $company;
public function __construct(Request $request)
{
$this->company = $request->attributes->get('company');
}
public function index()
{
dd($this->company); // returns null
}
}
This controllers index() returns null instead of the give model.
If I change the index() method to:
public function index(Request $request)
{
return $request->attributes->get('company');
}
This returns the model; as expected.
Why is this happening? It looks like the middleware is not run when the controller is constructed.... Is there a way to circumvent it?
Or am I missing the obvious here.....
I could off course repeat myself in each method; but that is not very DRY ;)
You can't access the session or authenticated user in your controller's constructor because the middleware has not run yet, So you can do it like this :
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->company = $request->attributes->get('company');
return $next($request);
});
}
For reasons currently unclear to me, the controller object is constructed before the request changes are reflected in the request object. In short the request is not considered properly constructed when a controller is constructed. This post seems to imply that.
There's two ways to work around this (if for a second we ignore what you're trying to do).
Use request dependency injection
public function index(Request $request)
{
$compary = $request->attributes->get('company');
}
This is not really WET because you're just swapping $this->company with $request->attributes->get('company') it's just a refactor. You should be injecting the request in the controller action anyway and if you don't want to do that you can use the request() helper.
Use a callback middleware in the constructor (Maraboc's answer explains how)
Now if you want a more case specific solution though you can use case specific dependency injection:
If you need to bind a model to a specific route parameter you can use route model binding and add the following in your RouteServiceProvider (or any provider).
Route::bind("companyAsARouteVarName", function () {
// this is why more details in the question are invaluable. I don't know if this is the right way for you.
//checks
// success
return $someModel;
});
Then you will register your route as:
Route::get("/something/{companyAsARouteVarName}", "SomeController#index");
and your controller will be:
public function index(Company $companyAsARouteVarName) {
//Magic
}
Controller constructor will be initialized before middleware execution.
You can get data from Injected $request object in controller functions.
So I have a Laravel Controller (MainController.php) with the following lines:
...
public function _settings_a(){
return view('_settings_a');
}
public function _settings_b(){
return view('_settings_b');
}
public function _settings_c(){
return view('_settings_c');
}
public function _settings_d(){
return view('_settings_d');
}
public function _staff_a(){
return view('_staff_a');
}
public function _staff_b(){
return view('_staff_b');
}
public function _staff_c(){
return view('_staff_c');
}
...
And my routes.php is as follows:
Route::any('_staff_a''MainController#_staff_a');
Route::any('_staff_b''MainController#_staff_b');
...
etc.
It seems there are a LOT of lines and a LOT of things to change if I change my mind...
I was wondering if I can have some regex in routes.php and an equivalent regex in MainController.php for handling routes that begin with an underscore (_)?
Can any Laravel experts share some tips/suggestions? I'm quite new to the framework.
Sure - just add it as a parameter. E.g. like this:
Route::any('_staff_{version}', 'MainController#_staff');
public function _staff($version) {
return view('_staff_'.$version);
}
I don't think you need to mess with regex. You can use implicit controllers Route::controller() which isn't the BEST solution, but will do what I think you are wanting.
So instead of
Route::any(..)
you can do
Route::controller('url', 'MainController');
So your route to whatever 'url' is will send you to this controller. Follow that with a '/' and then add whichever method in the controller you want to call.
Here is an example:
My url: 'http://www.example.com/users'
// routes.php
Route::controller('users', UserController');
// UserController.php
public function getIndex()
{
// index stuff
}
Now I send a request like: http://www.example.com/users/edit-user/125
// UserController.php
public function getEditUser($user_id)
{
// getEditUser, postEditUser, anyEditUser can be called on /users/edit-user
// and 125 is the parameter pasted to it
}
Doing it this way should allow you to be able to just send a request (post or get) to a url and the controller should be able to call the correct method depending on the url.
Here are some more rules about it: http://laravel.com/docs/5.1/controllers#implicit-controllers