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.
Related
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()));
});
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');
});
I have following route group for admin panel
Route::prefix('admin')->group(function (){
.
.
.}
I want to wrap this route to a new route e.g asda12asda
so that old behavior :
/admin/users
is changed to :
/asda12asda/users
not allowing old route. I don't want to change it internally from the system and want to find some efficient Laravel way to achieve it.
Redirect the old route to a new route
Route::prefix('admin')->group(function (){
Route::any('login', function () {
// Redirect to new route
redirect()->route('new route');
});
});
by creating a new route and mapping it accordingly
Route::prefix('asda12asda')->group(function () {
Route::any('login', function () {
// Do whatever you were about to do
})->name('new route');
});
If you are using Laravel 5.4 then you can add a new route file. Assume your route name is
adsp.php then add it to RouteServiceProvider.php like this.
protected function mapApiRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/adsp.php.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.
I'm using Laravel 5.1 and wanted to know if there is a better way to do routing redirection.
Route::get('user/login', 'UserController#login');
Route::get('login', function() {
return redirect()->to('user/login');
});
So basically, whenever a user goes to http://example.com/login, they will be redirect to http://example.com/user/login.
Is there a better way or other ways to do this or am I doing it correctly already? Thanks!
That's about as simple as it gets
You could also do redirect('user/login') to save a few characters
If you had multiple redirects like this you could handle them all at once
Route::pattern('user_path', '(login|logout)');
Route::get('{user_path}', function($user_path) {
return redirect('user/' . $user_path);
});
Route::get('new/create_view','CreateController#create_view');
Route::post('new/create_view','CreateController#insert_view');
Route::get('new/create_table','CreateController#create_table');
Route::post('new/create_table','CreateController#insert_table');
Route::get('new/create_package','CreateController#create_package');
Route::post('new/create_package','CreateController#insert_package');
This is the way i am using the route. Simple method. when the time of GET, am calling a controller function and inside that particular controller function, i have written the logical codes. In the POST also, doing the same thing.
Another way is there,GROUP Routing
Route::group(['namespace' => 'Admin'], function()
{
// Controllers Within The "App\Http\Controllers\Admin" Namespace
Route::group(['namespace' => 'User'], function()
{
// Controllers Within The "App\Http\Controllers\Admin\User" Namespace
});
});
eg:
Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
Route::resource('pages', 'PagesController', array('only' => array('index', 'store', 'show', 'update', 'destroy')));
Route::resource('users', 'UsersController');
});