I'm running this code to use my project policy for middlewares. It works as long as {project} is "last in the chain". Is there any way to make it work for deeper levels too?
Route::middleware(['web', 'auth:sanctum', 'verified'])->group(function () {
//...
Route::prefix('project/{project}')->middleware('can:view,project')->group(function () { // This works, but not if I go one more level after this...
Route::get('/', function (Project $project) {
return view('projects::show', [
'project' => $project,
]);
})->name('project');
Route::prefix('settings')->middleware('can:update,project')->group(function () {
// I get 403 here and I don't think I even get through the first middleware...
});
});
});
Either this
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::get('/', function (Project $project) {
return view('projects.show', [
'project' => $project,
]);
});
Route::group([
'prefix' => 'settings',
], function (Project $project) { // this is the one line I am not sure of
// some routes
})->can('update', Project::class);
})->can('view', Project::class);
or
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::get('/', function (Project $project) {
return view('projects.show', [
'project' => $project,
]);
});
Route::group([
'prefix' => 'settings',
], function () use ($project) { // this is the one line I am not sure of
Route::get('/', function (Setting $setting) use ($project) {
return view('project.settings.show', [
'project' => $project,
'settings' => $settings
]);
});
})->can('update', Project::class);
})->can('view', Project::class);
Way I think it should be done
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::middleware(['can:update, project'])->group(function () {
Route::get('settings', function() {
// do something
})
});
})->can('view', Project::class);
Related
Route::group(['controller' => [AdminController::class], 'middleware' => ['role:administrator']], function () {
Route::get('/admin', 'index');
});
It doesn't recognize the AdminController
Route::controller(AdminController::class)
->middleware('role:administrator'))
->group(function () {
Route::get('/admin', 'index');
});
More information about route controller groups in https://laravel.com/docs/9.x/routing#route-group-controllers
i'm validating a form with this:
public function update(UpdateExamenRequest $examenRequest, UpdateResultadoRequest $resultadoRequest)
{
$examenRequest->validated();
$resultadoRequest->validated();
This are the rules in UpdateExamenRequest
public function rules()
{
$this->redirect = url()->previous();
return [
'Paciente_id' => 'required|string|max:10',
'Examen_id' => 'required|string|max:10',
'Nombre' => 'required|string|max:50',
'Descripcion' => 'nullable|string',
];
}
public function messages()
{
return [
'Paciente_id.required' => __('Paciente_id.required'),
'Examen_id.required' => __('Examen_id.required'),
'Nombre.required' => __('Nombre.required'),
'Nombre.string' => __('Nombre.string'),
'Nombre.max' => __('Nombre.max'),
'Descripcion.string' => __('Descripcion.string'),
];
}
My routes:
Route::get('/', function () {
return view('auth.login');
})->middleware('guest');;
Route::get('/welcome', function () {
return view('welcome');
})->name('welcome');
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
require __DIR__ . '/auth.php';
Route::resource('pacientes', PacientesController::class)->middleware('auth');
Route::resource('examenes', ExamenesController::class)->middleware('auth');
Route::resource('resultados', ResultadoController::class)->middleware('auth');
i'm having a situation, and i've really don't know whats happening. The validation sends me to the show view, that i haven't created yet, there is a video: https://youtu.be/S-PtTdUH13Y
In my Laravel-5.8 application, I have a multi-company application using a single database. Each table have a company_id derived from the company table as shown below:
id | company_name | subdomain
1 | Main |
2 | Company1 | company1
3 | Company2 | company2
Main=> localhost:8888/myapp
Company1=>localhost:8888/company1.myapp
Company2=>localhost:8888/company2.myapp
I created a middleware:
class VerifyDomain
{
public function handle($request, Closure $next)
{
$domain == "myapp"; // your company app name
$path = $request->getPathInfo(); // should return /company1.myapp or /company2.myapp or /myapp
if (strpos($path, ".") !== false) { // if path has dot.
list($subdomain, $main) = explode('.', $path);
if(strcmp($domain, $main) !== 0){
abort(404); // if domain is not myapp then throw 404 page error
}
} else{
if(strcmp($domain, $path) !== 0){
abort(404); // if domain is not myapp then throw 404 page error
}
$subdomain = ""; // considering for main domain value is empty string.
}
$company = Company::where('subdomain', $subdomain)->firstOrFail(); // if not found then will throw 404
$request->session()->put('subdomain', $company); //store it in session
return $next($request);
}
}
Already, I have two (2) route groups in the route/web.php wgich looks like this:
Route::get('/', ['as' => '/', 'uses' => 'IndexController#getLogin']);
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// Config Module
Route::group(['prefix' => 'config', 'as' => 'config.', 'namespace' => 'Config', 'middleware' => ['auth']], function () {
Route::resource('countries', 'ConfigCountriesController');
Route::resource('nationalities', 'ConfigNationalitiesController');
});
// HR Module
Route::group(['prefix' => 'hr', 'as' => 'hr.', 'namespace' => 'Hr', 'middleware' => ['auth']], function () {
Route::resource('designations', 'HrDesignationsController');
Route::resource('departments', 'HrDepartmentsController');
Route::resource('employee_categories', 'HrEmployeeCategoriesController');
});
I have 2 issues:
If subdomain field is null, then the route should be for main domain: Main=> localhost:8888/myapp else localhost:8888/company1.myapp or localhost:8888/company2.myapp
2.How do I accomodate the route groups above into this:
Route::domain('localhost:8888/myapp')->group(function () {
Route::get('/', function ($id) {
//
});
});
Route::domain('localhost:8888/{subdomain}.myapp')->group(function () {
Route::get('/', function ($company_name, $id) {
$company = Company::where('subdomain', $subdomain)->firstOrFail();
// send the value of $company to data to send different view data
});
});
I'm not really sure that i understand you clearly. But, I hope you'll understand me :)
First thing is you're "domain". I suppose it's not real domain, but just uri. And maybe you should use it something like that:
Auth::routes();
$defaultDomain = config('myconfig.default_domain_name', 'myapp');
// I'm not reccomend to you use localhost:8888 here.
Route::domain('localhost:8888')
->group([
'middleware' => ['veryfy_domain'] // Add your VerifyDomain Middleware here
], function () {
// Here you already have a 'subdomain' param in session
// If you need special logic for default domain, you can out it here
Route::group(['prefix' => '/' . $defaultDomain], function () {
Route::get('/', function ($id) {
//
});
});
// Code below will work only with companies.
Route::group(['prefix' => '/{test}.' . $defaultDomain], function () {
Route::get('/', ['as' => '/', 'uses' => 'IndexController#getLogin']);
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// Config Module
Route::group(['prefix' => 'config', 'as' => 'config.', 'namespace' => 'Config', 'middleware' => ['auth']], function () {
Route::resource('countries', 'ConfigCountriesController');
Route::resource('nationalities', 'ConfigNationalitiesController');
});
// HR Module
Route::group(['prefix' => 'hr', 'as' => 'hr.', 'namespace' => 'Hr', 'middleware' => ['auth']], function () {
Route::resource('designations', 'HrDesignationsController');
Route::resource('departments', 'HrDepartmentsController');
Route::resource('employee_categories', 'HrEmployeeCategoriesController');
});
});
});
And about your middleware. I see it smth like that:
class VerifyDomain
{
public function handle($request, Closure $next)
{
$request->get('domain_name', $this->getBaseDomain());
$company = Company::where('subdomain', $subdomain)->firstOrFail();
$request->session()->put('subdomain', $company);
return $next($request);
}
// Better to store it in config
protected function getBaseDomain()
{
return config('myconfig.default_domain_name', 'myapp');
}
}
If you really want to use different domains, I think you need in your nginx something like this:
server_name *.myapp myapp;
And of course in your hosts file.
Than you can check it like that:
http://company.myapp
http://company1.myapp
http://myapp
Config example:
Create new file your_project_dir/app/config/myconfig.php (name it as you want)
Put this code in the file:
return [
'default_domain_name' => 'myapp'
];
Now you can use in in youre code as i suggest:
config('myconfig.default_domain_name');
I have the following code in my actions.js.
export const deleteUser = ({ dispatch }, payload) => {
console.log("El numero de id es js: ", payload.id);
return axios
.delete(window.urls.user.delete(payload.id))
.then(respone => {
dispatch("fetchUsers");
return Promise.resolve();
})
.catch(error => {
return Promise.reject(error);
});
};
api.php
Route::delete('/{user}', [
'uses' => 'UserController#destroy',
'middleware' => 'permission:user.delete',
'can:delete,user'
]);
config.blade.php
user: {
index : '/user',
store: '/user',
update: function(userid){
return '/user/' + userid
},
delete: function(id){
return '/user/' + id;
}
},
Controller
public function destroy(Request $request, $id)
{
$user = User::find($id);
$user->delete();
}
I think the problem is your user route definition. Assuming you don't use route grouping instead of
Route::delete('/{user}', [
'uses' => 'UserController#destroy',
'middleware' => 'permission:user.delete',
'can:delete,user'
]);
you should try:
Route::delete('/user/{user}', [
'uses' => 'UserController#destroy',
'middleware' => 'permission:user.delete',
'can:delete,user'
]);
I have a laravel route like below :
Route::group(['namespace' => 'Aggregate\Customer\Controller\v1_0','middleware' => 'jwt.auth', 'prefix' => 'api/v1.0/{lang}'], function () {
Route::put('customer/{id}', 'CustomerController#update_customer');
});
And i want to lang key on route 'prefix' => 'api/v1.0/{lang}' be first variable globally in all methods and in all controllers without manual added in all methods like :
See $lang
public function add_address_book($lang,$user_id,AddressBookRequest $request)
{
How can i do that?
One option is update the config var app.locale.
Route::group([
'namespace' => 'Aggregate\Customer\Controller\v1_0',
'middleware' => 'jwt.auth',
'prefix' => 'api/v1.0/{lang}'
], function () {
App::setLocale(app('request')->segment(3));
Route::put('customer/{id}', 'CustomerController#update_customer');
});
Then use
echo App::getLocale();
You can set the default locale and the fallback locale in app/config.php
Another option is to set up a singleton in the app container
Route::group([
'namespace' => 'Aggregate\Customer\Controller\v1_0',
'middleware' => 'jwt.auth',
'prefix' => 'api/v1.0/{lang}'
], function () {
app()->singleton('lang', function () {
return app('request')->segment(3);
});
Route::put('customer/{id}', 'CustomerController#update_customer');
});
Then in your controllers (or anywhere) you can use
echo app('lang');