How to get registered routes in Slim Framework 4 - slim-4

In Slim Framework 3 we can get all registered routes using this code
$routes = $this->container->get("router")->getRoutes();
Unfortunately this doesn't seem to work in slim 4. Is there any other way to get registered routes in slim 4 ?
thanks

After browsing the codes, found out that you can get all routes from the RouteCollectorProxy. Here's an example :
$routes = $app->getRouteCollector()->getRoutes();

Related

Laravel not picking up the correct route

I am developing an application using Laravel 8.x. I have an api.php file that I am using. It was all working fine for the last 5 months. Today I added a new route to the api.php file and that route is not getting picked up by the Laravel. I have the following items in the api.php file
//Products
Route::get('/products/{currency_id?}/{type?}/{eventtype?}', [Controllers\ProductsController::class, 'getproduct2']);
Route::get('/products/search/{query}/{itemtype?}/{itemcategory?}', [Controllers\ProductsController::class, 'findproduct']); //getproduct
Route::post('/products/findproductbyid/{product_id}', [Controllers\ProductsController::class, 'getproduct']);
Route::post('/products/create', [Controllers\ProductsController::class, 'setproduct']);
Route::post('/products/update', [Controllers\ProductsController::class, 'putproduct']);
And it shows up the URL list also
but when I access it (findproductbyid) in the browser it is not getting picked up. it shows the results of /products/{currency_id}/{type}/{eventtype}
Please tell me to know why is it so? I tried clearing Laravel cache, etc.
Thanks in advance
UPDATE: I tried changing the GET to POST, but it seems it is not getting updated to POST
You don't define url parameters with $, here you done {$product_id}
Change it to the following.
Route::get('/products/findbyproductid/{product_id}', [...]);
Rearrange the routes like this and try it out
Route::post('/products/findproductbyid/{product_id}', [Controllers\ProductsController::class, 'getproduct']);
Route::get('/products/search/{query}/{itemtype?}/{itemcategory?}', [Controllers\ProductsController::class, 'findproduct']); //getproduct
Route::get('/products/{currency_id?}/{type?}/{eventtype?}', [Controllers\ProductsController::class, 'getproduct2']);

Laravel Spark controller routes

I upgraded my Laravel Spark to version 6 and now I get the error ...
Class App\Http\Controllers\TeamController does not exist
This was not a problem previously, so I took a look at the routes files in /vendor/laravel/spark-aurelius/Http and compared them with previous versions. Up till version 5, the route was ...
$router->get('/'.$pluralTeamString.'', 'TeamController#all');
In version 6, the route is ...
$router->get('/settings/'.Spark::teamsPrefix(), 'TeamController#all');
I tried altering my routes file from this ...
$router->get('/teams', 'TeamController#all');
To several configurations of \settings\TeamController but couldn't get one to work. Am I missing something here?
Here are the pertinent parts of the Team Controller code:
namespace Laravel\Spark\Http\Controllers;
use Laravel\Spark\Spark;
use Illuminate\Http\Request;
use Laravel\Spark\Contracts\Repositories\TeamRepository;
class TeamController extends Controller
{
...
#joshua-foxworth Very likely you are trying to create the route on web.php under the routes folder. We know web.php by default checks for controllers under app -> Http -> Controllers folder. You are looking for this route file here from root dir spark/src/Http/routes.php Hope this solves your problem.

Middleware scoped to a route in MVC 6

How can be written middleware that is executed on specific routes using MVC 6? This can be done in the precious version of the WebAPI using a DelegatingHandler, I can't find a way of doing it in MVC 6 though.
something like this should do it I think
PathString path = new PathString("/somefolder");
app.Map(path,
appBranch =>
{
// add middleware needed for this branch
});

Working with Laravel routes

I built my application using Angularjs on the frontend and Laravel 5 at the backend, however my main issue now is routing, when the page is loaded initially I set it to return my angular.php view I even added some code to catch all routes and return that view for me.
This does not work in all cases:
routes.php
Route::any('{url?}', function($url) {
return view('angular');
})->where(['url' => '[-a-z0-9/]+']);
Example of a URL that works with this is:
http://localhost:8000/tickets/events/catgeories/
Example of a URL that does not work with this is:
http://localhost:8000/tickets/events/Musical/Some-event-name
By "not working" I mean Laravel throws a NotFoundHttpException. What I am thinking right now is the above route can't go past three levels/parameters as in /level-1/level-2/level-3.
What am I doing wrong here?
Maybe because second URL has uppercase characters?

Simplifying Routing Web Api 2 With Optional/Default Parameters

We have the following routes we are adding with Web Api MapHttpRoute method:
api/{version}/{namespace}/{controller}/{id}/{action}
api/{namespace}/{controller}/{id}/{action}
api/{version}/{namespace}/{controller}/{id}
api/{namespace}/{controller}/{id}
api/{version}/{controller}/{id}/{action}
api/{controller}/{id}/{action}
api/{version}/{controller}/{id}
api/{controller}/{id}
Is there anyway to add these as one or does it have to be 8 separate routes in order to work? we are using the namespace to mimick areas in web api and this is all working fine just would be great to reduce the number of routes.
I have tried setting RouteParameter.Optional and default values on the version, id and action. Also we have a constraint on the version but still I cannot get this working without creating 8 routes.

Resources