POST controller for all routes in Laravel 5 - laravel-5

I have callback button in header of my webpage, so user can send me message from every page.
How to make route for this? Something like that:
Route::post('{*}', 'PostController#callback');

It would be better if you do it via ajax.

Use middleware so you can check every request for certain post-data.
I would do it like this: create a file called MessageMiddleware.php in the directory App\Http\Middleware\
<?php namespace App\Http\Middleware;
use Closure;
class MessageMiddleware {
public function handle($request, Closure $next) {
if(isset($_POST['internal_message'])) {
// Do something so the message reaches you (db, email, whatever)
}
return $next($request);
}
}
?>
This is just a very basic version but should give you an idea.
You will not have to register any routes for this and the middleware will work for all urls the middleware is registered for.
If you want a middleware to be run during every HTTP request to your application, simply list the middleware class App\Http\Middlware\MessageMiddleware in the $middleware property of your app/Http/Kernel.php class.
The official Laravel documentation for Middlware is very extensive and does certainly not only cover authentication middleware.

Doing some try and check I found that the most simple route allows sending callback request from every page!!!
Route::get('/', ['as' => 'home', 'uses' => 'HomeController#index']);
Route::post('/', ['as' => 'callback', 'uses' => 'PostController#callback']);
But I don't know why. If someone know why please tell me, because I really want to know the background.
I was also trying to do it using middleware as I was advised. It was also working solution. Messages were sending from every page, but with message I got 403 error code in console every time. And of course I was trying to get rid of that.
With this simple solution it works without any errors in console.

Related

How can I test the Api in Laravel via Postman?

I have a route like this:
Route::group(
[
'prefix' => 'v1', 'as' => 'v1.',
],
static function () {
Route::get('test', 'Api\TestController#test')->name('test');
///
How can I refer to this route so that it would work? I am trying to call this route in Postman http://localhost/api/v1/test but I get a 400 error
You didn't provide enough information but check these just to be sure
Make sure these routes are listed among the api routes and not the web
If you're using artisan to serve your project, then ensure you're using the correct port in postman too eg http://127.0.0.1:8000/api/v1/test
(Pretty obvious) Make sure you're sending a get request from postman
Check the function too to know if there's any problem. You could do an early return of a simple json response in the beginning of the function to make sure the route accessed the function at all

Laravel - GET request download logout automaticlly

i have a download file link like this:
Download file
ROUTES
Route::group(['middleware' => ['web','auth','Admin','active'], 'prefix' => 'admin'], function(){
// USERS
Route::resource('user','UserController');
Route::post('user/permissions/update','UserController#update_permission')->name('update_user_permissions');
// MODULI
Route::resource('module','ModuleController');
// MODULISITICA
Route::resource('modulistica','ModulisticaController');
Route::post('modulistica_cliente','ModulisticaController#update_client_module')->name('modulistica_post_cliente');
Route::post('modulistica_prodotto','ModulisticaController#update_product_module')->name('modulistica_post_prodotto');
Route::get('modulistica/download/cliente/{file}','ModulisticaController#download_cliente')->name('modulistica_download_cliente');
Route::get('modulistica/download/{file}','ModulisticaController#download_module')->name('modulistica_download_module');
Route::get('modulistica/download/prodotto/{file}','ModulisticaController#download_prodotto')->name('modulistica_download_prodotto');
// UTILITY
Route::post('utility/become/client','UtilityController#become_client')->name('utility_become_client');
Route::resource('loan','LoanController');
Route::get('area_download/document/{file}', function ($file){
$path_file = storage_path().'/app/public/documents/'.$file;
return response()->download($path_file, $file);
})->name('download_document');
});
ERROR
Arrival at the "https://mysite.it/admin/loan" view without problems. When I click on the GET link it redirects me to the LOGIN, but being my user logged in by login redirects me to "https://mysite.it/home".
I did some tests getting the following information:
Request does not arrive at route "area_download / document / {file}"
The request does not arrive at the 'Admin', 'active' middlewares.
So my conclusions are that the problem is in the middleware "Web" or "Auth" but I can not understand why. Place the entire group of routes, if it can be useful. If you need more on the routes, I can attach screenshots!
I would appreciate your help thank you!
If you just allow downloading a file without any authentication then,
You can try this :
Blade File
Download file
From this user can directly download the file. Just need to add file path in href and download attribute.
or else remove the middleware AUTH if you don't want to Authenticate the user.
And you want to authenticate the user then need route:list and middleware details.
I found the solution! the problem was that my get request was made this way.
https://mysite.it/admin/area_download/document/example.pdf
the final PDF extension creates system error. Avoiding the final extension such as:
https://mysite.it/admin/area_download/document/example.pdf/go
Problem solved!

Best way to use middleware Auth in Laravel to redirect

So I'm upgrading a project from Laravel 5.0 to 5.4 and I've just stumble across the method now depreciated beforeFilter which was used this way :
function __construct() {
$this->beforeFilter('auth', array('only'=>array('showDashboard')));
}
Which basically say (from my understanding) if you'r not logged in yet you can still use the method showDashBoard.
So I'm trying to do something similar.
I've tried $this->middleware('auth', ['except' => ['showDashboard']]); but without success.
I tough of extracting the middleware from the construct and putting it directly in the routes but this not seems to be a clean solution for me.

Vue SPA url not working with history mode enabled

I am creating a SPA using Vue js and it kind of works but I have one problem. With history mode enabled on Vue I can enter urls and go to that page using the Vue Router but when I try to login I get the literal page html. I know I can do something like auth\{vue?} but I would prefer if i didn't have to do that. I want to be able to always keep the url with no prefixes unless it is to an API request. So for example:
I have the root view:
Route::get('/{vue?}', function () {
return view('layouts.app');
})->where('vue', '[\/\w\.-]*');
and then I have the api requests:
Route::group(['prefix' => 'api'], function () {
Route::post('/login', 'Auth\\LoginController#login');
Route::post('/register', 'Auth\\RegisterController#register');
Route::get('/logout', 'Auth\\LoginController#logout');
});
but when I hit /api/login I get the return view data from /{vue?}.
I hope that makes sense and if so any help would be amazing, Thanks.
it is clearly explained here: https://kjamesy.london/work/laravel-53-vuejs-20-make-vuerouters-history-mode-play-nicely-with-laravels-routes
Short story: declare history mode to false in VueRouter and then on the Laravel side, whenever the $request->ajax() is false, the same route will always capture the request. So, we can safely define a catch-all route to redirect such traffic to the index() method of our resource controller:

Laravel 5.2 Session not passing

I have a simple success message on store to DB.
\Session::flash('info', 'Success! Words created');
now if I var_dump the session and return that, great.
As soon as I move to another view. Session info is gone!
I've tried all sorts, I'm on laravel 5.1.
looked into the middleware groups but i just get blank pages when adding routes in here..
This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errors variable available to all your views is not being utilized because it was moved from the global middleware to the web middleware group.
There are two ways to fix this:
In your kernel.php file(app/Http/Kernel.php), you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.
Wrap all your web routes with a route group and apply the web middleware to them:
Route::group(['middleware' => 'web'], function() {
// Place all your web routes here...(Cut all `Route` which are define in `Route file`, paste here)
});

Resources