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

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::*'.

Related

Laravel url alias, mismatch in url generation

I have a Laravel 5.2 application where I want to display the same page on 2 different domains / routes. I have it working using the following route structure:
The routes to my primary domain:
Route::group(['domain' => 'www.primarydomain.com',
'prefix' => 'demo-page']), function(){
Route::get('/my-page', 'MyController#index');
Route::get('/my-second-page', 'MyController#getPageTwo');
}
The routes to my secundary domain (note: no prefix!):
Route::group(['domain' => 'www.secundarydomain.com',]), function(){
Route::get('/my-page', 'MyController#index');
Route::get('/my-second-page', 'MyController#getPageTwo');
}
The idea is that both routes will work, and they do. Both www.secundarydomain.com/my-page and www.primarydomain.com/demo-page/my-page work.
The issue is when I now want to generate a link to my second page. For building my URL's in my views, I'm using the following function to generate a link to my-second-page:
url('/my-page')
This function always generates a link to www.primarydomain.com/my-page, while I need a link to www.primarydomain.com/demo-page/my-page.
Is there any easy solution to resolve this? Can this be resolved using middleware, or will a custom URL function be needed?
Expected results:
url('my-page') on www.primarydomain.com should generate a link to www.primarydomain.com/demo-page/my-page
url('my-page') on www.secondarydomain.com should generate a link to www.secondarydomain.com/my-page
Easiest way to do that is to create your own helper, like custom_url() and use it instead of url().
You can look how original url() helper works and create similar one. Look here:
https://github.com/laravel/framework/blob/5.3/src/Illuminate/Foundation/helpers.php#L806
You can assign aliases to your routes.
Route::group(['domain' => 'www.primarydomain.com', 'prefix' => 'demo-page']), function(){
Route::get('/my-page', [
'as' => 'my_page_prefixed',
'uses' => 'MyController#index'
]);
Route::get('/my-second-page', [
'as' => 'my_second_page_prefixed'
'uses' => 'MyController#getPageTwo'
]);
}
And then you can call your aliased route on your blade templates by using {{ route('my_page_prefixed') }} or any other alias.

Laravel project structure

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
}
]);

Laravel localize url helper

I need to localize my website. I am using this package: https://github.com/mcamara/laravel-localization
My route group is:
Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController#garage']);
//OTHER ROUTES
});
And i want to localize link to this route. If i am using
href="{{ route('garage') }}
everything is ok, link looks like "www.example.com/locale/garage".
But if i am using
href="{{ url('/garage') }}
my localization doesn't work, link looks like "www.example.com/garage".
Is there some way to localize links made with URL helper?
LaravelLocalization package includes a middleware object to redirect all non-localized routes to the corresponding "localized".
So, if a user navigates to http://www.example.com/test and the system has this middleware active and 'en' as the current locale for this user, it would redirect him automatically to http://www.example.com/en/test.
To active this functionality go to app/Http/Kernel.php and add these classes in protected $routeMiddleware = [] at the beginning and then in your routes file bring these changes:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController#garage']);
//OTHER ROUTES
});
And your problem will be solved.

Laravel generate the routes using Route::resource need to add prefix /admin/

For example , I have a routes that is for the admin page to manage books, a route is set like this:
Route::resource('books','Admin\BookController');
It generated few routes for insert / update/ delete etc... automatically
/books/create
/books/1/edit
The problem is , it is admin page and I would like the link to be
/admin/books/create
/admin/books/1/edit
How to specific the resource to be admin one ? it auto have prefix of /admin/ Thanks
Updated:
If you need prefix for a multiple routes, you should use route group:
Route::group(['prefix' => 'admin'], function()
{
Route::resource('books','Admin\BookController');
});
Or, if you need to use just one controller, you could just do this:
Route::resource('/admin/books','Admin\BookController');
Change your route to
Route::resource('admin/books','Admin\BookController');
Just to add to Alexey's Answer. I'm using Namespace too with group. Below is the example.
Route::group([
'prefix' => 'admin',
'namespace' => 'Admin',
'middleware' => 'admin.routeNeedsPermission:view-admin-management',
], function() {
Route::resource('books','BookController');
});
By that way you don't need to write admin in all your routes.

Laravel 5 - 3 forms on the same page

I am beginer with Laravel, I need help please.
In my edit user page, I have 3 forms.
But I can not do in my rooter how to make it work.
My code (in Routes) :
<?php
// update role
Route::post('edit/{id}', [
'as' => 'user_post_update_role',
'uses' => 'UserController#updateRole'
]);
// update infos
Route::post('edit/{id}', [
'as' => 'user_post_update',
'uses' => 'UserController#update'
]);
// update password.
Route::post('edit/{id}', [
'as' => 'user_post_update_password',
'uses' => 'UserController#updatePassword'
]);
?>
But the last rule, Block others.
How to do ?
Thank
The only way to do this, would be to have a single route, and have the controller that handles that route identify which form was submitted.
What you're basically doing with those routes, is defining three routes that for all intents and purposes, are identical. Therefore, it'll only use the last one that was created as each new one, overwrites its predecessor.
Alternatively, you could do as it says in the comment on your post, use edit/{id} for updating user info, edit/password/{id} for updating password, and edit/role/{id} for updating the role. You could even break these out into separate methods entirely, or separate sections.
Of the many ways you can achieve this, your chosen method is unfortunately not amongst them.
Edit your route like this:
// update role
Route::post('edit/{id}/role', [
'as' => 'user_post_update_role',
'uses' => 'UserController#updateRole'
]);
// update infos
Route::post('edit/{id}/main', [
'as' => 'user_post_update',
'uses' => 'UserController#update'
]);
// update password.
Route::post('edit/{id}/password', [
'as' => 'user_post_update_password',
'uses' => 'UserController#updatePassword'
]);
So you have unique routes and don't need complicate if else statements.

Categories