Moving on to next route if some url cannot be found - laravel-5

I am working on an application using Laravel 5.4, VueJS, AXIOS, VueRouter. VueRouter is responsible for all of the navigation in the application, save for the route config below:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Auth::routes();
Route::get('/{catchall?}', ['as' => 'start', 'middleware' => 'auth', function() {
return view('home')->with([...]);
}])->where('catchall', '.*');
This essentially redirects the user to the 'base' blade file, which contains the <router-link></routerlink>.
However, I am now implementing a page in my application that anyone can access (not requiring the auth middleware), and utilizes a different layout entirely (so needs a new blade file).
Preferably, it would be accessed like this:
app.sample.com/{some-string}
Rather than something else like this:
app.sample.com/survey/{some-string}
{some-string} is a randomly generated string that is then thrown into the DB. accessible by one of my models. As I understand it, I could do something like this:
Route::get('/{some-string}', 'SomeController#somemethod')
Placing this above the catchall should work. However, in the case that /{some-string} does not match anything in the DB, I would prefer that it gets handled by the catchall, which will serve the home.blade.php
Is there any clean way to handle this?

You're essentially describing the RedirectIfAuthenticated middleware, at least thats how I would approach this. Utilize middleware to check a user's authentication level and do any route matching before serving them through to the application.

Related

How to add custom middleware to Laravel Nova

I am not 100% expert with Laravel routes, so I am finding some issues trying to accomplish the following:
on each an every Nova page requested, I need to first verify that a token is obtained and valid on an external service (it expires every 30').
I have a controller handle managing that and working fine. My problem is that I don't hit the nail with adding this verification to a middleware.
In other words, before displaying any Nova view, I need to check that this token is not expired. If it is, I post a call and refresh it.
I've googled around and found no similar question/issue.
Anyone can give me an enlighting example?
Thanks in advance.
I found the place while looking at another post here.
What I was missing was to declare my token validator class in config/nova.php
Here:
`/*
|--------------------------------------------------------------------------
| Nova Route Middleware
|--------------------------------------------------------------------------
|
| These middleware will be assigned to every Nova route, giving you the
| chance to add your own middleware to this stack or override any of
| the existing middleware. Or, you can just stick with this stack.
|
*/
'middleware' => [
'web',
Authenticate::class,
DispatchServingNovaEvent::class,
BootTools::class,
Authorize::class,
HereShouldGoMyClass::class,
],`

Laravel Jetstream Inertia non authorised route

I have a fairly stock install of the Laravel, jetstream inertia js setup as shown here https://jetstream.laravel.com/2.x/installation.html using command php artisan jetstream:install inertia --teams
I want to show the dashboard to non logged in users. I am using a route without the auth:sanctum', 'verified' middleware, and trying to get the controller to set up the data that the inertia stack needs to fire up when the user IS logged in:
public function getPageDashboard(){
return Inertia::render('Dashboard', [
'user' => Auth::user()
]);
}
but I'm getting a lot of errors from the front end. "Error in render: "Error: Ziggy error: 'team' parameter is required for route 'teams.show'.""
Basically sanctum? jetstream? inertia? ziggy? sets up a lot of stuff somewhere in the middleware that I have not figured out.
Any ideas on my approach here?
Edit:
Here is the web route file:
<?php
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use App\Http\Controllers\ViewController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/dashboard', [ViewController::class, 'getPageDashboard'])->name('dashboard');
Your request will fail to overwrite user, if you want to get the authenticated user in your view you can use $page.props.user

defining route with 'auth:web' middleware in laravel service provider boot() method

im using a package called https://github.com/garygreen/pretty-routes
there is line in its service provider boot() method (here the code)
it is defining a get route with middlewares from its config file(link to the code) I just added 'auth:web' to its config file but it seems the 'auth:web' middleware is called as soon as code reaches the line before Laravel bootstraps its session and etc. when the auth('web')->user() is yet null
What I can not understand is that I do the same exact thing (here the code)with laravel/telescope but it works. why ???
also changing :
Route::get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
to :
$this->app['router']->get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
in service provider seems to solve the problem and make this code behave like the way telescope package use 'auth:web' as middleware.
what's happening ?
You need to have the web middleware applied to any routes you need sessions for, which is what the default authentication system is using. When you apply the auth middleware without this it can't possibly resolve a user since there is no session to be authenticated against.
You need to apply the web middleware and then what ever other middleware you want:
'middlewares' => [
'web', 'auth:web',
],
If you look at the telescope example you provided you will see they also add the web middleware. So you didn't quite "do the same exact thing" as the telescope config.

POST controller for all routes in 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.

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