Resource route generating strange model name from an 'ax' ending model - php

I registered this resource:
Route::resource('order-item-paxes', 'OrderItemPaxController', ['except' => ['show', 'create', 'store']]);
The problem is that I cannot get the model in the controller doing this:
public function edit(OrderItemPax $order_item_pax)
{
$order_item_pax = OrderItemPax::find($id);
return view('production.order-item-paxes.edit', compact('order_item_pax'));
}
$order_item_pax->toArray() returns an empty Array.
I checked the routes through php artisan route:list and its returning something strange:
PUT|PATCH | production/order-item-paxes/{order_item_paxis}
It should be order_item_pax instead of order_item_paxis.
Any idea?
UPDATE
If I use $order_item_paxis in my controller it works. I've registered hundreds of Resources and I've always used the singular version of the name

You can tell Laravel to override the route parameters by including parameters array in the $options array (3rd param):
Route::resource('order-item-paxes', 'OrderItemPaxController', [
'except' => ['show', 'create', 'store'],
'parameters' => ['order-item-paxes' => 'order_item_pax']
]);
Hope this helps!

Related

Laravel 5.x How to obtain the controller name using a route name?

I am trying to get the controller name using a route name.
I have a route ['dashboard'] and I will like to get the controller name to later execute a method on the same controller.
I read the documentation but could find a method or way.
https://laravel.com/api/5.7/Illuminate/Routing/Route.html
Any suggestion will be very appreciated.
As an example, this will give you information for the register route:
Route::getRoutes()->getByName('register')->action;
This will give you an array of all the information you should need:
[
"middleware" => [
"web",
],
"uses" => "App\Http\Controllers\Auth\RegisterController#showRegistrationForm",
"controller" => "App\Http\Controllers\Auth\RegisterController#showRegistrationForm",
"namespace" => "App\Http\Controllers",
"prefix" => null,
"where" => [],
"as" => "register",
]
If you're doing this alot, you can add a macro in your RouteServiceProvider:
public function register()
{
Route::macro('getByName', function($name) {
return $this->getRoutes()->getByName($name);
});
}
and now you can simply do
Route::getByName('register') to get all the route information.
You could try with:
get_class(\Request::route()->getController());

CakePHP 3.4 and CakePHP v3.5 Parameterized routing

I am making small project in cakePHP v3.5, i am not able to use parametrized in appropriate way. Also in some cases if we want to pass optional parameter in url then how can i do?
CakePHP v3.5
$routes->get('test/testfn/:param1/:param2', ['controller' => 'Pages', 'action' => 'testfn']);
where
test: Controller,
testfn: Method of TestController,
param1: Parameter 1,
param2: Parameter 2
All i did to get params from url to TestController,
$this->request->getParam(param1)
$this->request->getParam(param2)
How can i list all parameters that i passed from routes to My Controller instead of single param step by step.
OR anyone have better options to do routing in cakePHP v3.5
Also I am confused about the paramterized routing principle of cakePHP3.4
so, in that case if anyone has some solution to cakePHP v3.4.
Please Help me.
Thanks
In config/routes.php
$routes->get(
'/api/test/*', ['controller' => 'Api', 'action' => 'check']
);
In controller
public function check($first=null, $sec=null) {
pr($params);
pr($sec);
die;
}
In Routes.php
$routes->connect('/users/:id/edit/:type', ['controller' => 'Users', 'action' => 'edit', ['id' => '\d+', 'pass' => ['id', 'type'], '_name' => 'edit-client']);
In this route in ID is user id and Type is user type two parameter pass in this route
if this route method is POST then ex - $this->request->getData('id'); and if this route method GET then ex - $this->request->getParam('id')

how to change url name while using resource route in laravel

Normally while using resource route for example like this:
Route::resource('somethings','SomethingsController' );
The url here which is displayed in browser in http://localhost:8000/somthings/create but what want is to display like this:
http://localhost:8000/somthings basically I dont want create in the url.
You can't change URL while using Route::resource(). You'll need to define all routes manually:
Route::get('somethings', 'SomethingsController#createSomething');
https://laravel.com/docs/9.x/controllers#restful-localizing-resource-uris
App\Providers\RouteServiceProvider
file, boot add the following;
public function boot()
{
Route::resourceVerbs([
'create' => 'oluştur',
'edit' => 'düzenle',
]);
// ...
}
To change route names in resource:
Route::resource('somethings', 'SomethingsController', [
'names' => [
'index' => 'somethings.index',
'store' => 'somethings.store',
...
]
]);

Named Routes Conflict Laravel 5.2

I am creating a Multilingual Laravel 5.2 application and I am trying to figure out how to change language when I already have the content.
routes.php
...
Route::get('home', [
'as' => 'index',
'uses' => 'SiteController#home'
]);
Route::get([
'as' => 'index',
'uses' => 'SiteController#inicio'
]);
...
I have SiteController#home and SiteController#inicio. So I change session('language') in SiteController#change_language like:
...
public function change_language ($lang){
session(['language' => $lang]);
return redirect()->action(SAME NAMED ROUTE, DIFFERENT LANGUAGE);
}
...
So, When I click on a button with
English
from /inicio (SiteController#inicio) I should be redirected to the same named route (SiteController#home) so I can check the language and display appropriate content.
Any ideas of how to get the named route or something helpful?
Thank you :)

Laravel rename routing resource path names

Can we rename routing resource path names in Laravel like in Ruby on Rails?
Current
/users/create -> UsersController#create
/users/3/edit -> UsersController#edit
..
I want like this;
/users/yeni -> UsersController#create
/users/3/duzenle -> UsersController#edit
I want to do this for localization.
Example from Ruby on Rails;
scope(path_names: { new: "ekle" }) do
resources :users
end
I know this is an old question. I'm just posting this answer for historical purposes:
Laravel now has the possibility to localize the resources. https://laravel.com/docs/5.5/controllers#restful-localizing-resource-uris
Localizing Resource URIs By default, Route::resource will create
resource URIs using English verbs. If you need to localize the create
and edit action verbs, you may use the Route::resourceVerbs method.
This may be done in the boot method of your AppServiceProvider:
use Illuminate\Support\Facades\Route;
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot() {
Route::resourceVerbs([
'create' => 'crear',
'edit' => 'editar',
]); }
Once the verbs have been customized, a resource route registration such as Route::resource('fotos', 'PhotoController') will
produce the following URIs:
/fotos/crear
/fotos/{foto}/editar
It ain't pretty, but you could define multiple routes that use the same controller function. For example:
Route::get("user/create", "UsersController#create");
Route::get("user/yeni", "UsersController#create");
The only (glaringly obvious downside) being that you're routes will get quite cluttered quite quickly. There is a setting in app/config/app.php where you can set/change your locale, and it could be possible to use that in conjunction with a filter to use the routes and then group those routes based on the current local/language, but that would require more research.
As far as I know, there isn't a way to rename resource routes on the fly, but if you get creative you can figure something out. Best of luck!
You can't change the resource url's.
For this you will need to define/create each route according your needs
Route::get("user/yeni", "UsersController#create");
and if you need more than one languages you can use the trans helper function, which is an alias for the Lang::get method
Route::get('user/'.trans('routes.create'), 'UsersController#create');
I just had the same issue. And managed to recreate some sort of custom resource route method. It probably could be a lot better, but for now it works like a charm.
namespace App\Helpers;
use Illuminate\Support\Facades\App;
class RouteHelper
{
public static function NamedResourceRoute($route, $controller, $named, $except = array())
{
$routes = RouteHelper::GetDefaultResourceRoutes($route);
foreach($routes as $method => $options) {
RouteHelper::GetRoute($route, $controller, $method, $options['type'], $options['name'], $named);
}
}
public static function GetRoute($route, $controller, $method, $type, $name, $named) {
App::make('router')->$type($named.'/'.$name, ['as' => $route.'.'.$method, 'uses' => $controller.'#'.$method]);
}
public static function GetDefaultResourceRoutes($route) {
return [
'store' => [
'type' => 'post',
'name' => ''
],
'index' => [
'type' => 'get',
'name' => ''
],
'create' => [
'type' => 'get',
'name' => trans('routes.create')
],
'update' => [
'type' => 'put',
'name' => '{'.$route.'}'
],
'show' => [
'type' => 'get',
'name' => '{'.$route.'}'
],
'destroy' => [
'type' => 'delete',
'name' => '{'.$route.'}'
],
'edit' => [
'type' => 'get',
'name' => '{'.$route.'}/'.trans('routes.edit')
]
];
}
}
Use it like this in the routes.php:
\App\Helpers\RouteHelper::NamedResourceRoute('recipes', 'RecipeController', 'recepten');
Where the first parameter is for the named route, second the controller and third the route itself.
And something like this to the view/lang/{language}/route.php file:
'edit' => 'wijzig',
'create' => 'nieuw'
This results in something like this:
This is not possible in Laravel as they use code by convention over configuration. A resources uses the RESTfull implementation
Therefore you have to stick to the convention of
GET /news/create
POST /news
GET /news/1
GET /news/1/edit
...

Categories