Using Route Prefixes in Lumen - php

From the Lumen 5.2 docs:
The prefix group attribute may be used to prefix each route in the
group with a given URI. For example, you may want to prefix all route
URIs within the group with admin:
$app->group(['prefix' => 'admin'], function () use ($app) {
$app->get('users', function () {
// Matches The "/admin/users" URL
});
});
My code:
$app->group(['prefix' => 'v1'], function () use ($app) {
$app->get('lessons', function () {
['as' => 'lessons.index', 'uses' => 'LessonsController#index'];
});
});
This returns a 200 but it is clearly not calling the index() method on the LessonsController.
I have also tried this:
$app->group(['prefix' => 'v1'], function () use ($app) {
$app->get('lessons', ['as' => 'lessons.index', 'uses' => 'LessonsController#index']);
});
Results in ReflectionException in Container.php line 738: Class LessonsController does not exist

I am currently using prefixes like this:
$app->group(['namespace' => "App\Http\Controllers", 'prefix' => 'v1'], function($app){
$app->get('/lessons', 'LessonsController#index');
});
Which works fine in my version of Lumen. You would access the url /v1/lessons and it is handled by the index() method inside the LessonsController
Note: It would appear that the Lumen documentation misses out that in order to do this you require the 'namespace' => "App\Http\Controllers" key value pair in order for this to work.

Related

How to make sub-routes with prefix but can work without prefix in Laravel?

I'm building a multilingual Website using Laravel but I'm facing a problem about Locales.
I have 2 Languages for now (Ar/En) and my routes accept prefix to determine the Language.
I want my routes to be valid if not having a prefix and set a default Locale.
my current code is :
Route::group([
'prefix' => '/{locale?}',
'where' => ['locale' => '^(ar|en)$'],
'middleware' => ['setLocale']
], function(){
Route::get('/', function () {
return view('home');
});
Route::get('test', function (){
return 'test';
});
});
It works for the first route but for any sub-routes its not working if prefix is not provided!
You could define a fallback route
Route::fallback(function () {
abort_if(in_array(request()->segment(1), ['ar', 'en']), 404);
return redirect()->to(url(app()->getLocale().request()->getPathInfo()));
});

fetching route from vendor through routes/web.php

I have this route included in my web.php in my routes folder.
Route::group(['prefix' => 'admin','namespace'=>'Admin', 'middleware' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'filemanager'], function () {
\UniSharp\LaravelFilemanager\Lfm::routes();
});
});
My issue is whenever I run php artisan route:list I get the following error:
Class App\Http\Controllers\Admin\UniSharp\LaravelFilemanager\Controllers\LfmController does not exist
The Lfm controller is inside my vendor folder and I've been scouting around the internet for a solution and applied many different ways for the route change and the only way I got it to work is by using the route provided by default from the package. But if I use that, I use lose my admin authentication I'm hoping someone could give me some insight as to what I'm doing wrong here?
I would really appreciate that! Thank you in advance.
I came up with this solution. I am using Laravel 5.4
Instead of putting the code in routes/web.php, put it in RouteServiceProvider, so the default namespace is not set yet;
protected function mapWebRoutes()
{
Route::group(['prefix' => 'laravel-filemanager', 'middleware' => ['web', 'auth']], function () {
\UniSharp\LaravelFilemanager\Lfm::routes();
});
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
}
For laravel 5.6 and above,
Go to config/lfm.php and set 'use_package_routes' to false.
In routes/web.php use the following routes (this is an example, might change adhering to your prefix etc.):
Route::group(['prefix' => 'admin','namespace'=>'Admin', 'middleware' => 'admin', 'as' => 'admin.'], function () {
Route::get('/filemanager', '\UniSharp\LaravelFilemanager\Controllers\LfmController#show')->name('show');
Route::any('/filemanager/upload', '\UniSharp\LaravelFilemanager\Controllers\UploadController#upload')->name('unisharp.lfm.upload');
Route::get('/filemanager/errors', '\UniSharp\LaravelFilemanager\Controllers\LfmController#getErrors')->name('getErrors');
Route::get('/filemanager/jsonitems', '\UniSharp\LaravelFilemanager\Controllers\ItemsController#getItems')->name('getItems');
Route::get('/filemanager/move', '\UniSharp\LaravelFilemanager\Controllers\ItemsController#move')->name('move');
Route::get('/filemanager/domove', '\UniSharp\LaravelFilemanager\Controllers\ItemsController#move')->name('domove');
Route::get('/filemanager/newfolder', '\UniSharp\LaravelFilemanager\Controllers\FolderController#getAddfolder')->name('getAddfolder');
Route::get('/filemanager/folders', '\UniSharp\LaravelFilemanager\Controllers\FolderController#getFolders')->name('getFolders');
Route::get('/filemanager/crop', '\UniSharp\LaravelFilemanager\Controllers\CropController#getCrop')->name('getCrop');
Route::get('/filemanager/cropimage', '\UniSharp\LaravelFilemanager\Controllers\CropController#getCropimage')->name('getCropimage');
Route::get('/filemanager/cropnewimage', '\UniSharp\LaravelFilemanager\Controllers\CropController#getNewCropimage')->name('getCropimage');
Route::get('/filemanager/rename', '\UniSharp\LaravelFilemanager\Controllers\RenameController#getRename')->name('getRename');
Route::get('/filemanager/resize', '\UniSharp\LaravelFilemanager\Controllers\ResizeController#getResize')->name('getResize');
Route::get('/filemanager/doresize', '\UniSharp\LaravelFilemanager\Controllers\ResizeController#performResize')->name('performResize');
Route::get('/filemanager/download', '\UniSharp\LaravelFilemanager\Controllers\DownloadController#getDownload')->name('getDownload');
Route::get('/filemanager/delete', '\UniSharp\LaravelFilemanager\Controllers\DeleteController#getDelete')->name('getDelete');
Route::get('/filemanager/demo', '\UniSharp\LaravelFilemanager\Controllers\DemoController#index')->name('getDelete');
});

Laravel route Resource breaks another route

When I use my route like this, the test route is working fine.
Route:-
Route::group(array('namespace' => '\User'), function () {
Route::get('user/my-favorite','UserController#myFavorite');
Route::get('user/test','UserController#test'); // is working
Route::resource('user', 'UserController');
Route::group(['middleware' => ['auth']], function () {
Route::get('user/planed',['as' => 'user.planed', 'uses' => 'USerController#planed']);
.
.
.
.
But when I want to use it like following, it shows a blank page :
Route::group(array('namespace' => '\User'), function () {
Route::get('user/my-favorite','UserController#myFavorite');
Route::resource('user', 'UserController');
Route::group(['middleware' => ['auth']], function () {
Route::get('user/planed',['as' => 'user.planed', 'uses' => 'UserController#planed']);
Route::get('user/test','UserController#test'); // is not working
.
.
.
.
What is my mistake?
Because the call to test is intercepted by
Route::resource('user', 'UserController');
because if you check the routes in the console with
$> php artisan route:list
you'll see it includes
GET|HEAD | user/{user}
and then your first route
Route::get('user/test','UserController#test'); // is not working
is never reached. Try to put it BEFORE the other line.
I thing the issue in naming the route check This
Try like this
Route::group(array('namespace' => '\User'), function () {
Route::get('user/my-favorite','UserController#myFavorite');
Route::resource('user', 'UserController');
Route::group(['middleware' => ['auth']], function () {
Route::get('user/planed','UserController#planed'])->name('user.planed');
Route::get('user/test','UserController#test'); // is not working
Just replace
Route::get('user/planed',['as' => 'user.planed', 'uses' => 'UserController#planed']);
With
Route::get('user/planed','UserController#planed'])->name('user.planed');

Skipping a middleware for a particular route inside a route group in Laravel

I Want to skip the middleware for a particular route in a route group. How can I do this?
Route::group(['prefix' => 'testgroup','middleware' => ['login.oauth']],function (){
Route :: get('/', 'testController#index');
Route :: get('/api','testController#apiCall');
});
I want to skip the 'login.oauth' middleware for the Route :: get('/api','testController#apiCall')
Please keep that testgroup function must be accessible to all routes and middleware function to particular(some other route) in the same function
Route::group(['prefix' => 'testgroup'], function () {
Route::group(['middleware' => ['login.oauth'], function() {
Route :: get('/', 'testController#index');
});
Route :: get('/api','testController#apiCall');
});
Just create a second group without the middleware:
Route::group(['prefix' => 'testgroup','middleware' => ['login.oauth']],function (){
Route :: get('/', 'testController#index');
});
Route::group(['prefix' => 'testgroup'],function (){
Route :: get('/api','testController#apiCall');
});

Prefix all the routes in Lumen

Is there a way in Lumen to prefix all of my routes?
The thing is that I'm versioning my API via URI and for every group that I create I have to set the prefix to 'v1/*' like:
$app->group(['prefix' => 'v1/students/', 'namespace' => 'App\Http\Controllers\Students\Data'], function () use ($app) {
$app->get('/', 'StudentController#get');
$app->get('/{id}', 'StudentController#getByID');
});
Apparently, route groups in Lumen do not inherit any settings, which was intentional to keep the router simpler and faster (see comment here).
Your best bet will probably be to create a route group per version, in order to define a base prefix and controller namespace for that version. But, your individual routes inside those route groups will need to be slightly more verbose. Example shown below:
// creates v1/students, v1/students/{id}
$app->group(['prefix' => 'v1', 'namespace' => 'App\Http\Controllers'], function () use ($app) {
$app->get('students', 'Students\Data\StudentController#get');
$app->get('students/{id}', 'StudentController#getByID');
});
// creates v2/students, v2/students/{id}, v2/teachers, v2/teachers/{id}
$app->group(['prefix' => 'v2', 'namespace' => 'App\Http\Controllers'], function () use ($app) {
$app->get('students', 'Students\Data\StudentController#get');
$app->get('students/{id}', 'Students\Data\StudentController#getByID');
$app->get('teachers', 'Teachers\Data\TeacherController#get');
$app->get('teachers/{id}', 'Teachers\Data\TeacherController#getByID');
});
You could prefix all routes in your /bootstrap/app.php.
Currently, there should be something like
# may be slighlty different, since I typed this from memory
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
require __DIR__ . '/../routes/web.php';
});
As you can see, this loads the web.php file and makes the $router variable available.
You can rewrite this to load any php file in the routes directory and prefix all those routes via
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
// load all files in routes directory, prefix all of them
$globalPrefix = "/v1";
$router->group(["prefix" => $globalPrefix], function($router) {
$routes = glob(__DIR__ . '/../routes/*.php');
foreach ($routes as $route) require $route;
});
});
As you can see, all required routes are wrapped in $router->group with your application wide route prefix.

Categories