laravel9: how to get full route name - laravel-9

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()

Related

In cakephp 3.6, How to change user finder query for auth component?

For cakephp 3.6, cakephp.org tell how to customise user finder query for auth component at following link:
link
But I am not getting how to implement it?
I have 'department_id' column in users table which belongs to departments table. I want to change the following query:
public function findAuth(){
$query
->select(['id', 'username', 'password'])->contain(['Departments'])
->where(['Users.active' => 1]);
return $query;
}
Will the above code work?
Please tell me in which file I have to write the function?
And what are other necessary steps to get it done, so that I get all user related info in $this->Auth->User ?
Firstly, you will need to load the Auth component and pass in custom configuration in any controller that you want to use the custom finder like so:
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'finder' => 'auth'
]
],
]);
}
then, in your UsersTable you would have the custom finder:
public function findAuth(\Cake\ORM\Query $query, array $options)
{
$query
->select(['id', 'username', 'password'])->contain(['Departments'])
->where(['Users.active' => 1]);
return $query;
}
This answer may also help Containing tables in custom Auth finder

'auth' Middleware with Route::resource

How can I use middleware with resources?
Route::resource('myitem', ['middleware' => 'auth', 'uses' => 'App\\Controllers\\MyitemsController']);
Just followed https://laracasts.com/discuss/channels/general-discussion/struggling-with-routeresource-and-auth-middleware but unfortunately could not solve.
Getting error:
ErrorException (E_UNKNOWN)
Array to string conversion
Open: /vendor/laravel/framework/src/Illuminate/Routing/Router.php
protected function getResourceAction($resource, $controller, $method, $options)
{
$name = $this->getResourceName($resource, $method, $options);
return array('as' => $name, 'uses' => $controller.'#'.$method);
}
Using filter with resource was not working that why had to use Route::group
Route::group(array('before' => 'auth'), function()
{
Route::resource('myitem', 'App\\Controllers\\MyitemsController');
});
https://stackoverflow.com/a/17512478/540144
Middleware is a new feature of Laravel 5. In Laravel 4, filters where something similar. So instead of using the key middleware you should use before or after. Also, and that's where the error comes from, the second argument of Route::resource should be the controller name as string and the third one is an array of options:
Route::resource('myitem', 'App\\Controllers\\MyitemsController', ['before' => 'auth']);
Edit
Apparently before filters only work with resource routes when you wrap a group around it. See the OPs answer for an example...
I just came up against this and found the easiest way is to add the middleware straight to the controller.
I found my answer here:
http://laravel.com/docs/master/controllers
class MyitemsController extends Controller {
/**
* Instantiate a new MyitemsController instance.
*/
public function __construct()
{
$this->middleware('auth');
}
}
How to do this in Laravel 5. The Answer you have been waiting for.
Use middleware instead of before
Route::group(array('middleware' => 'auth'), function()
{
Route::resource('user', 'UserController',
['only' => ['edit']]);
}
To check if the route is setup, run:
php artisan route:list
which should show the following:
GET|HEAD | user/{user}/edit | user.edit | App\Http\Controllers\UserController#edit | auth
Note auth instead of guest
Better solution
Use middleware instead of before
Route::group(['middleware' => 'auth'], function(){
Route::resource('myitem', 'MyitemsController');
});
You can check if it's ok with:
php artisan route:list

Laravel's dingo/api with controllers instead of routes

How can I use Controllers instead of Routes when with dingo/api (https://github.com/dingo/api) package?
The documentation (https://github.com/dingo/api/wiki) uses routes only.
Dingo is just an extension of the Laravel router, so you can use controllers just like you do in any other Laravel project.
So this:
Route::api(['version' => 'v1', 'prefix' => 'api'], function()
{
Route::get('posts', function()
{
return Post::all();
});
});
would become:
Route::api(['version' => 'v1', 'prefix' => 'api'], function()
{
Route::get('posts', 'PostsController#index');
});
Do you mean connecting a controller to a specific dingo route? like this:
$api = app('Dingo\Api\Routing\Router');
then followed by
$api->version('v1', function($api){
$api->get('posts', 'PostsController#index');
});
But somehow Dingo cannot find the directory of controller, so what I do is start from App\ directory like this: App\Http\Controllers\PostsController#index .
so the correct syntax would be
$api->get('posts', 'App\Http\Controllers\PostsController#index');

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