changing the default model binding key in resource route definition in laravel 9 - laravel-9

is there any way to change the default model binding key just for some of the methods in resource route definition?
I try to use the parameters method like this
Route::resource('category', CategoryController::class)->names([
'index' => 'site.category.index',
'show' => 'site.category.show',
'update' => 'site.category.update',
'edit' => 'site.category.edit',
'create' => 'site.category.create',
'store' => 'site.category.store'
])->parameters([
'category' => 'ca_slug'
]);
But when i use that method, all of actions model key goes into same.
While i want to change just model key of some of those like (show or edit)

Related

laravel9: how to get full route name

i have this from my routes/web.php
Route::group([
'middleware' => 'user',
'prefix' => 'currency',
'as' => 'currency-'
], function (){
Route::get('/', [CurrencyController::class, 'index'])->name('currency');
Route::get('add', [CurrencyController::class, 'add'])->name('add');
Route::get('edit/{id}', [CurrencyController::class, 'edit'])->name('edit');
Route::post('add', [CurrencyController::class, 'form_add']);
});
i need to get the route with format the prefix name and the route name. So the result will be like currency-currency and currency-add.
can i do something like Route::PrefixRoute()?
you can access current request route name using one of the below methods.
public function index(Request $request){
dd($request->route()->getName());
}
or
request()->route()->getName()
or
import use Illuminate\Support\Facades\Route; so you can access like
Route::currentRouteName()

Why does Laravel authentication require the 'web' guard?

I have setup Laravel 5.5 and installed the default authentication scaffolding.
My application has two types of users - customers and staff - so I prefer to name the authentication guards that way, and the following configuration seems to work.
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'customers' => [
'driver' => 'session',
'provider' => 'customer-users',
],
'staff' => [
'driver' => 'session',
'provider' => 'staff-users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
My providers, customer-users and staff-users, use the standard eloquent driver, however they each return a different user type.
The problem is that I would like to remove the 'web' guard, since it's just cluttering the config file. When I do however, I get an exception somewhere deep in the Laravel middleware.
I can live with the extra clutter of course, but it does bother me that Laravel is relying on a configuration item that I can't change. Is this a Laravel bug, perhaps?
BTW - I don't have 'web' set as the default guard when I get the error...
By default web guard given by laravel which used for web authentication based on session driver with the table users. Now you have created your own custom guards and you are using it. So your wish to keep that web guard. But if you remove, you might be facing some internal issue, so best you keep it and it's not going to be the performance issue too.

Laravel : Webhook class not found - Captain Hook

I'm trying to use captain-hook package to make webhook in my laravel app so that I can send a notification to a specific user when an eloquent event is fired.
I followed the package's documentation and put this code at the end of the method that will fire the event :
Webhook::create([
"url" => Input::get("url"),
"event" => "eloquent.saved: \App\DoctorRequest",
"tenant_id" => $user->id
]);
but I get this error :
FatalThrowableError in DoctorRequestController.php line 109:
Class 'App\Http\Controllers\Webhook' not found
How can I fix it ?
You haven't imported this in your controller.
Assuming you've added the Service Provider in your config/app.php file then you just need to import it at the top of your controller with:
use Webhook;
If you don't wish to import the Facade then you can reference it like this instead:
\Webhook::create([
"url" => Input::get("url"),
"event" => "eloquent.saved: \App\DoctorRequest",
"tenant_id" => $user->id
]);

Route not defined when using 'auth' flter

I've started playing around with Laravel. I want to create simple authentication logic using 'auth' filter for my route. The problem is when I set the route like this:
Route::get('/user', array('before' => 'auth', 'as' => 'user', function() {
return Redirect::action('UserController#index');
}));
Route::get('/login', 'UserController#login');
I get: [InvalidArgumentException] - Route [UserController#index] not defined
However, when I go with a basic route:
Route::get('/user', 'UserController#index');
the page renders successfully.
Can anyone see the problem?
You could try adjusting the route to use UserController#index
Route::get('/user', array(
'before' => 'auth',
'as' => 'user',
'uses' => 'UserController#index'
));

phalcon currently dispatching route name

I use custom routes which include namespace besides controller and action. So for ACL purposes I use MVC route name as ACL resource name. Now I need to obtain currently DISPATCHING route name. The only solution I've come up with is to get namespace/controller/action from Dispatcher and iterating over all the routes find an appropriate one.
Is there any easiest way to obtain currently dispatching (not just matched) route name?
Pretty easy
\Phalcon\DI::getDefault()->get('router')->getMatchedRoute()->getName();
You can use your router, dispatcher and base controller to get what you need. Consider this:
$router = new \Phalcon\Mvc\Router(false);
$routes = array(
'/{namespace:"[a-zA-Z]+}/:controller' => array(
'controller' => 2,
),
'/{namespace:"[a-zA-Z]+}/:controller/:action/:params' => array(
'controller' => 2,
'action' => 3,
'params' => 4,
),
);
foreach($routes as $route => $params) {
$router->add($route, $params);
}
Now in your base controller you can do this:
public function getNamespace()
{
return $this->dispatcher->getParam('namespace');
}
This way you can have the namespace currently being served in your controllers (so long as they extend your base controller).
If you need to get the namespace in a model you can always use the DI like so (base model):
public function getNamespace()
{
$di = \Phalcon\DI::getDefault();
return $di->dispatcher->getParam('namespace');
}