I am new in laravel framework. Can anyone tell me how to delete sessions when cms pages accessed i.e(faq,privacy policy,about us). This query runs fine for me:-
$request->session()->forget('key');
The problem is that when i accessed the faq page i have write this query and when i accessed the privacy policy then again i have to write this query. Can anyone tell me how i do in one function. So i have not implement this query again and again
Thanks in advance :)
Create One middleware named as forgetSession(you can have any name) and set the cms pages route group in app\Http\routes.php under that middleware for eg.
Route::group(['middleware' => ['forgetSession']], function () {
Route::resource('faq', 'faqController');
Route::resource('privacy', 'privacyController');//likewise
});
Now create middleware by writing below command on cmd project root
php artisan make:middleware forgetSession
So it will create the middleware in app/Http/middleware/forrgetSession
and put your code
$request->session()->forget('key');
So in this way all route mentioned under route group will have the code to forget session. This way definitely you can redundant the code.
you can remove session at your controller
call this in controller contruct method
$request->session()->forget('key'); //remove by key
$request->session()->flush(); // remove all
Related
I had 2 user types in my laravel app and have now added a third.
Im trying to add the 3rd user type's middleware into an existing function at the top of my controller.
Below was my code:
public function __construct()
{
$this->middleware('admin')
->except('route', 'route2');
}
Now I have added another middleware to this but that causes a 404 page:
$this->middleware(['admin', 'viewer'])
->except('route', 'route2');
I also tried this but it said it needed to be an array so this doesn't work:
$this->middleware(['admin', 'viewer'])
->except('route', 'route2');
The issues isn't with the middleware itself I don't think because if I swap the middleware name when Im just passing one it works like and shows the expected view, not a 404:
$this->middleware('viewer')
->except('route', 'route2');
Thanks!
Try this;
$this->middleware(['admin', 'viewer'])
->except(['route', 'route2']);
and which laravel version are you using? Also you can check this documentation https://laravel.com/docs/8.x/controllers#restful-partial-resource-routes
it appears that when I created a new route, I receive the 404 error when trying to access the url, which is funny,. because all of my other routes are working just fine.
My web.php looks like so:
Auth::routes();
Route::post('follow/{user}', 'FollowsController#store');
Route::get('/acasa', 'HomeController#index')->name('acasa');
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Route::get('/profil/{user}/edit', 'ProfilesController#edit')->name('editareprofil');
Route::patch('/profil/{user}', 'ProfilesController#update')->name('updateprofil');
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Route::get('/alerte/url/{user}', 'UrlsController#index')->name('editurl');
Route::post('/alerte/url/{user}', 'UrlsController#store')->name('updateurl');
Route::get('/alerte/url/{del_id}/delete','UrlsController#destroy')->name('deleteurl');
The one that is NOT working when I am visiting http://127.0.0.1:8000/alerte is:
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The controller looks like so:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class PaginaAlerte extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function index(User $user)
{
return view('alerte');
}
}
I am banging my head around as I cannot see which is the problem. It is not a live website yet, I am just developing on my Windows 10 pc using WAMP.
Moved my comment to a little bit explained answer.
So, in your route collection, you have two conflicting routes
Route::get('/{user}', 'ProfilesController#index')->name('profil');
and
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Imagine that Laravel is reading all routings from top to bottom and it stops to reading next one after the first match.
In your case, Laravel is thinking that alerte is a username and going to the ProfilesController#index controller. Then it tries to find a user with alerte username and returning 404 because for now, you don't have a user with this username.
So to fix 404 error and handle /alerte route, you just need to move the corresponding route before /{username} one.
But here is the dilemma that you got now. What if you will have a user with alerte username? In this case, the user can't see his profile page because now alerte is handling by another route.
And I'm suggesting to use a bit more friendly URL structure for your project. Like /user/{username} to handle some actions with users and still use /alerte to handle alert routes.
The following route catches the url /alerte as well
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Since this one is specified before
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The /alerte will go the the ProfilesController instead.
To fix this change the order of the url definitions or change either of the urls to have nesting e.g. /alerte/home or /user/{user}
Well.
Maybe this is too late, but I have all week dealing with this problem.
I made my own custom.php file and add it in the routes path of my Laravel project, and none of the routes were working at all.
This is how I solved it:
You must remember to edit the RouteServiceProvider.php file located in app\Providers path. In the map() function, you must add your .php file. That should work fine!
To avoid unexpected behaviors, map your custom routes first. Some Laravel based systems can "stop" processing routes if no one of the expected routes rules were satisfied. I face that problem, and was driving me crazy!
I would wish suggest to you declare your URL without the "/", like your first "post" route, because sometimes, I have been got this kind of errors (404).
So, my first recomendation is change the declaration of the route. After that, you should test your middleware, try without the construct, and try again.
Good luck!
I'm building enterprise modular Laravel web application but I'm having a small problem.
I would like to have it so that if someone goes to the /api/*/ route (/api/ is a route group) that it will go to an InputController. the first variable next to /api/ will be the module name that the api is requesting info from. So lets say for example: /api/phonefinder/find
In this case, when someone hit's this route, it will go to InputController, verifiy if the module 'phonefinder' exists, then sends anything after the /api/phonefinder to the correct routes file in that module's folder (In this case the '/find' Route..)
So:
/api/phonefinder/find - Go to input controller and verify if phonefinder module exists (Always go to InputController even if its another module instead of phonefinder)
/find - Then call the /find route inside folder Modules/phonefinder/routes.php
Any idea's on how to achieve this?
Middlewares are designed for this purpose. You can create a middleware by typing
php artisan make:middleware MiddlewareName
It will create a middleware named 'MiddlewareName' under namespace App\Http\Middleware; path.
In this middleware, write your controls in the handle function. It should return $next($request); Dont change this part.
In your Http\Kernel.php file, go to $routeMiddleware variable and add this line:
'middleware_name' => \App\Http\Middleware\MiddlewareName::class,
And finally, go to your web.php file and set the middleware. An example can be given as:
Route::middleware(['middleware_name'])->group(function () {
Route::prefix('api')->group(function () {
Route::get('/phonefinder', 'SomeController#someMethod');
});
});
Whenever you call api/phonefinder endpoint, it will go to the Middleware first.
What you are looking for is HMVC, where you can send internal route requests, but Laravel doesn't support it.
If you want to have one access point for your modular application then you should declare it like this (for example):
Route::any('api/{module}/{action}', 'InputController#moduleAction');
Then in your moduleAction($module, $action) you can process it accordingly, initialize needed module and call it's action with all attached data. Implement your own Module class the way you need and work from there.
Laravel doesn't support HMVC, you can't have one general route using other internal routes. And if those routes (/find in your case) are not internal and can be publicly accessed then also having one general route makes no sense.
I am using Laravel 5.3
I have two models, Parent and Student with a many-to-many relationship between them and I want to add a Parent for a Student and vice versa.
My approach was:
Create a link from Student profile to add Parent, like so:
Add Parent
Add the route students.parents.add with a controller method to flash the id to the session and redirect.
// in web.php:
Route::get('/students/{id}/parents/add', ['as' => 'students.parents.add', 'uses' => 'StudentController#addParent']);
Route::resource('students', 'StudentController');
// and in StudentsController:
public function addParent($id)
{
return redirect()->route('parents.create')->with('associated_id', $id);
}
After that, clicking the button redirects to the ParentsController create() but the session data is not there when I try return session()->all().
Am I missing something?
Make sure you are not adding web middleware in your routes.php file as it is already added in the file RouteServiceProvider.php file.
If you now apply it again in your routes.php, you will see that web appears twice on the route list php artisan route:list. This exactly makes the flash data discard.
I have a website in laravel framework and I am trying to add a simple new static page to the admin panel. I have done the following three steps:
Add a template to the views:
app/views/admin/MessageToAll.blade.php
Add the make view code in the controller.
public function MessageToAll(){
return View::make('admin.MessageToAll');
}
Added a route in app/routes.php
Route::get('/admin/MessageToAll',array('as'=>'MessageToAll','uses'=>'AdminController#MessageToAll'));
But when I go to to domain.com/admin/MessageToAll
it gives me a 404 page not found error. Does anyone know what have I missed as I think I have completed all steps for adding this view.
Just put your new route before the /admin/ route (to test it, you want to temporarily make it the very first route in routes.php). The problem is /admin/ or some other similar route executed before your new route.
Also, if you need to just execute static view, you can use something like this (works without using a controller):
Route::get('/admin/MessageToAll', function (){
return View::make('admin.MessageToAll');
});
in routes add:
Route::get('/admin/MessageToAll','yourController#yourMethod');