Laravel project structure - php

I am building a system where the user creates a project and then the project has pages and routes.
I am working out the URL structure now and in Laravel is there a good way with middle ware to get the project information. Here's what I am thinking for the route structure:
{project}/
{project}/something
{project}/something/else
{project}/settings
Route::group with a prefix seems like the best way to include all the middleware. Is there a way I can write a middleware class to check if the {project} exists and then send the data to the view?
Route::group([
'prefix' => '{project}',
'middleware' => [
'auth',
'getprojectstuffandthings'
], ], function () {
// project routes
}
]);

Related

Remove Laravel Package routes and add new

I want to remove Laravel Package routes and add new ones.
I installed this package https://github.com/jeremykenedy/laravel-roles on my Laravel 6.X application, I wanted to disable the routes so I would like to add a prefix of admin/ before the set routes...
Changing the routes in vendor folder doesn't help because it will get updated and gone if you run composer update, any idea on how to solve this issue, other than forking the repo?
I didn't have time to actually scan through all of the environment variables that you can configure in the package. I would recommend to check if there are any config values that you can override first. However, if you can't, then i am sure you could copy the routes and put it in your web.php
<?php
/*
|--------------------------------------------------------------------------
| Laravel Roles And Permissions Web Routes
|--------------------------------------------------------------------------
|
*/
Route::group([
'middleware' => ['web'],
'prefix' => 'admin',
'as' => 'laravelroles::',
'namespace' => 'jeremykenedy\LaravelRoles\App\Http\Controllers',
], function () {
// Dashboards and CRUD Routes
Route::resource('roles', 'LaravelRolesController');
Route::resource('permissions', 'LaravelPermissionsController');
// Deleted Roles Dashboard and CRUD Routes
Route::get('roles-deleted', 'LaravelRolesDeletedController#index')->name('roles-deleted');
Route::get('role-deleted/{id}', 'LaravelRolesDeletedController#show')->name('role-show-deleted');
Route::put('role-restore/{id}', 'LaravelRolesDeletedController#restoreRole')->name('role-restore');
Route::post('roles-deleted-restore-all', 'LaravelRolesDeletedController#restoreAllDeletedRoles')->name('roles-deleted-restore-all');
Route::delete('roles-deleted-destroy-all', 'LaravelRolesDeletedController#destroyAllDeletedRoles')->name('destroy-all-deleted-roles');
Route::delete('role-destroy/{id}', 'LaravelRolesDeletedController#destroy')->name('role-item-destroy');
// Deleted Permissions Dashboard and CRUD Routes
Route::get('permissions-deleted', 'LaravelpermissionsDeletedController#index')->name('permissions-deleted');
Route::get('permission-deleted/{id}', 'LaravelpermissionsDeletedController#show')->name('permission-show-deleted');
Route::put('permission-restore/{id}', 'LaravelpermissionsDeletedController#restorePermission')->name('permission-restore');
Route::post('permissions-deleted-restore-all', 'LaravelpermissionsDeletedController#restoreAllDeletedPermissions')->name('permissions-deleted-restore-all');
Route::delete('permissions-deleted-destroy-all', 'LaravelpermissionsDeletedController#destroyAllDeletedPermissions')->name('destroy-all-deleted-permissions');
Route::delete('permission-destroy/{id}', 'LaravelpermissionsDeletedController#destroy')->name('permission-item-destroy');
});
If you want to keep your web.php clean, then create a file at routes/laravel-permissions.php and then in your RouteServiceProvider load the routes as follow,
protected function mapLaravelPermissionRoutes()
{
Route::prefix('admin')
->as('laravelroles::')
->middleware('web')
->namespace('/jeremykenedy\LaravelRoles\App\Http\Controllers')
->group(base_path('routes/laravel-permissions.php'));
}
If you do load it this way, then make sure that you remove the grouping from your laravel-permissions.php i.e. remove the following code
Route::group([
'middleware' => ['web'],
'prefix' => 'admin',
'as' => 'laravelroles::',
'namespace' => 'jeremykenedy\LaravelRoles\App\Http\Controllers',
], function () {
Now you just need to map the method as follow
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
$this->mapLaravelPermissionRoutes();
}

Understanding laravel 'auth' and 'web' middleware

I have strange behavior on one of route groups I have defined e.g
Route::group(['prefix' => 'admin', 'middleware' => ['web','auth']], function (){
//admin routes
}
);
Whenever I defined 'middleware' => ['web','auth'], routes are accessible after login , but without 'web' I am redirected to / home.
What I thought was that , 'web' is default middleware injected ,please correct me if I am wrong. Otherwise there is some other setting that is affecting this behavior ?
Laravel comes with web middleware groups that contains common middleware you may want to apply to web UI routes. the web middleware group is automatically applied to your default routes.php file by the RouteServiceProvider.
The web middleware is default now, but if you have create laravel app skeleton a while ago you have to update the route provider . You can see the changes in this diff

Use Laravel Spark's Controllers/Actions in my app's API

I'm up and running with Laravel Spark, but I'd like to pull some of the user auth methods (and later, some others) in to my API.
Spark's default registration method is a POST request to /register that calls Auth\RegisterController#register.
I would like registration to be POST request to api/v1/register but for the sake of simplicity, I'd like to simply call Spark's Auth\RegisterController#register method.
I did try simply copying the RegisterController from Spark in to my app's controller directory, but that didn't seem like an elegant solution and it didn't work anyway.
My app\Http\api.php contains the following group:
Route::group([
'prefix' => 'api/v1',
'middleware' => 'auth:api'
], function () {
Route::get('register', 'Auth\RegisterController#showRegistrationForm');
Route::post('register', 'Auth\RegisterController#register');
});
I'd love input and advice on the best way to pull in some of those Spark methods that I get out of the box.
Thanks in advance!

how to prevent some route tracking in antonioribeiro/tracker laravel package

I am using antonioribeiro/tracker laravel package to Store stats.
Now,I have a routes for Admin directory like this :
Route::group(
array (
'middleware' => 'IsAdmin',
'as' => 'admin::'
),
function () {
Route::get('desktop', [ 'uses' => 'DesktopController#index']);
//some Other Routes
}
);
And I do not want to track this routes and sub routes.
For that I change do_not_track_routes option like this :
'do_not_track_routes' => [
'admin.*',
'tracker.stats.*',
],
But seems this does not work and with every visits Admin or sub Admin directories , add new sessions (or visits) to tracker_sessions table.
You've specified not to track admin.*, but your routes are going to be named admin::*.
You need to either change your route group to be 'as' => 'admin.', or you need to change your do_not_track_routes to exclude 'admin::*'.

Laravel route starting with a specific string

I need to create a route with some fixed and dynamic parts. Basically I need to be flexible on the second segment on the url. If the url starts with 'products/test....' then the route has to go to the PageController, all other routes starting with 'products/....' have to go to the ProductController.
// Something like this:
Route::any('products/".starts_with($slug, 'test'), [
'uses' => 'PageController#show'
])->where('slug', '(.*)?');
Route::get('products/{slug}', [
'uses' => 'ProductController#show'
]);
Is this possible in Laravel 5?
In Laravel 5 we use Middleware as helpers for routes.
There are some examples in the default installation that you can adapt for your code. This is the best approach for this issue.

Categories