View::composer not working on boot() service provider - laravel

Inside a Laravel Package EspacePartenaire I need to share some data between all its views using View Composer.
Ths boot() method inside EspacePartenaireServiceProvider containes the following:
$this->loadViewsFrom(__DIR__ . '/views', 'espace-partenaire');
View::composer('*', CurrentPartenaireComposer::class);
But I don't want to share data in all the views. I need it only in the views that are located in the view folder of the Package /packages/EspacePartenaire/src/views
When I change arguments of the composer function like below:
View::composer('espace-partenaire', CurrentPartenaireComposer::class);
or
View::composer('espace-partenaire::*', CurrentPartenaireComposer::class);
I had the error that my variables are undefined.
How can I achieve this?
EDIT:
This is the Package routes file:
Route::group([
'middleware' => ['web', 'auth'],
'namespace' => 'App\Services\EspacePartenaire\Http\Controllers',
'prefix' => 'espace-partenaire'
], function(){
...
Route::get('/', 'EspacePartenaire#index');
...
});

Based on your packages structure i think the following approach could maybe work, it seems first parameter is the path and you can do * as a wildcard.
View::composer('*espace-partenaire*', CurrentPartenaireComposer::class);

Related

Can't seem to find a way to use the route.php from 5.2 from laravel 5.2 to 8.83.25 web.php

I'm pretty new with Laravel, was able to work on finding a tutorial but it uses a
5.2 version.
I'm trying to convert the older version to 8.83.25
This is the route in the tutorial that I'm following.
I have created the CategoryController.php manually
Route::group(['middleware' => ['web']], function(){
Route::get('category', 'CategoryController');
});
What you used is wrong syntax. To pass a route to a controller, you are supposed to pass it as an array as such:
Route::group(['middleware' => ['web']], function(){
Route::get('category', [CategoryController::class]);
});
Now supposing you want to pass it to a particular function(lets say the function store) in your controller, you just indicate that in the array as such:
Route::group(['middleware' => ['web']], function(){
Route::get('category', [CategoryController::class, 'store']);
});
Take a look at the docs to learn more about laravel v8 routing.

Why does the route name products not work?

I use Laravel with Vue.
I added the following route:
Route::patch('/products/updateOrder', 'Product\ProductController#updateOrder')->name('updateOrder');
I added it in the block:
Route::group([ 'namespace' => 'App\Http\Controllers\Api\BasicData', 'prefix' => 'basicData', 'middleware' => ['role:basicDataAdmin']], function () {
Route::patch('/products/updateOrder', 'Product\ProductController#updateOrder')->name('updateOrder');
Route::get('/products/{product}/productProcesses', 'Product\ProductProcessController#index');
Route::get('/products/{product}/productProcesses/{process}', 'Product\ProductProcessController#show');
Route::post('/products/{product}/productProcesses/{process}', 'Product\ProductProcessController#store');
Route::put('/products/{product}/productProcesses/{process}', 'Product\ProductProcessController#update');
Route::delete('/products/{product}/productProcesses/{process}', 'Product\ProductProcessController#destroy');
Route::resource('/products', 'Product\ProductController')->except(['updateOrder']);
Route::resource('/workplaces', 'Workplace\WorkplaceController');
Route::resource('/partNames', 'PartName\PartNameController');
Route::resource('/processes', 'Process\ProcessController');
});
I get the following error:
"message": "No query results for model [App\\Models\\BasicData\\Product\\Product] updateOrder",
Update order function:
public function updateOrder(Request $request)
{
Product::setNewOrder($request->productIds);
}
I tried to change the order of the routes. Same problem.
When i add an x to the name like this Route::patch('/productsx/updateOrder' it works.
I thought that it's the order of the routes. But it isn't.
Is the only way to solve it to change the name products?

How to add simple view other than Crud to Laravel Backpack

I'm trying to figure out how to add a simple view (without CRUD) to Laravel Backpack. I've found this article, however I can't make it work.
I have added new controller and view, however I'm getting following error:
Method App\Http\Controllers\Admin\RaportyController::setupRoutes does not exist.
I don't understand the routes, what should I add to make it work?
Thank you
EDIT
I've changed the ::crud to ::get in my routes and I'm getting a different error: App\Http\Controllers\Admin\RaportyParkingoweController is not invokable.
My routes (custom.php) file:
<?php
// --------------------------
// Custom Backpack Routes
// --------------------------
// This route file is loaded automatically by Backpack\Base.
// Routes you generate using Backpack\Generators will be placed here.
Route::group([
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => array_merge(
(array) config('backpack.base.web_middleware', 'web'),
(array) config('backpack.base.middleware_key', 'admin')
),
'namespace' => 'App\Http\Controllers\Admin',
], function () { // custom admin routes
Route::crud('opinie', 'OpinieCrudController');
Route::crud('rezerwacje', 'RezerwacjeCrudController');
Route::crud('uzytkownicy', 'UzytkownicyCrudController');
Route::get('raporty', 'RaportyController');
}); // this should be the absolute last line of this file
It should be a simple fix, point to the method you want in that controller: when you're using Route::get(), make sure to also specify the method you want to point to. It should be Route::get('RaportyParkingoweController#yourMethodName');, not Route::get('RaportyParkingoweController');.

Laravel 7 automatic scoping in resource route

is there a way to use the new automatic scoping in resource routes? Everything I tried didn't work:
Route::apiResource('instances/{instance:id}/projects', 'ProjectController', [
'except' => ['destroy']
]);
The following manual solution is working but would be a mess in the routes file.
Route::get('instances/{instance:id}/project/{project:id}',function(Instance $instance, Project $project){
return response()->json($project);
});
Thanks
I had a similar problem and as far as I can tell, the custom route key needs to be specified on the nested resource to trigger automatic scoping. The easiest way to do it without specifying each route separately is probably using parameters():
Route::apiResource('instances.projects', 'ProjectController')->parameters([
'projects' => 'project:id'
]);
it is probably help you
Route::group(['prefix' => 'instances/{instance:id}'], function () {
Route::apiResource('project', 'ProjectController', [
'except' => ['destroy']
]);
});
it could be an issue your models are in some other directory. you have to resolve the models first to be injected.
public function boot()
{
parent::boot();
Route::model('instance', App\Instance::class);
Route::model('project', App\Project::class);
}
You have to explicitly bind the models to keys.
Route::get('instances/{instance:id}/project/{project:id}',function(Instance $instance, Project $project){
return response()->json($project);
});

User defined Function works in Route::get() but not Route::group()

I am using laravel 7 and created a function to include all route files includeRouteFiles() in a directory. Im just trying to figure out why this works:
Route::get('/', function () {
includeRouteFiles(__DIR__ . '/someFolder/');
});
and this does not:
Route::group(['namespace' => 'SomeSpace'], function() {
includeRouteFiles(__DIR__ . '/someFolder/');
});
this is located in the web.php file under the routes directory.
I assume the that the code in the ServiceProvider where the functions lives is correct because it works in Route::get().
thank you

Resources