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',
...
]
]);
Related
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());
I have the following code where I want to find a model using a field called link instead of id. However, it doesn't seem to produce any results. Where could I be getting it wrong? It returns 404
public function actionView($link)
{
$model = News::find()->where(['link'=>$link])->all();
return $this->render('view', [
'model' => $model,
]);
}
NB: in the search model, I have tried adding this:
$query->andFilterWhere([
'id' => $this->id,
'link'=>$this->link,
'category' => $this->category,
'date' => $this->date,
'userid' => $this->userid,
'featured' => $this->featured,
]);
If you want a single model you need one() and not all()
public function actionView($link)
{
$model = News::find()->where(['link'=>$link])->one();
return $this->render('view', [
'model' => $model,
]);
}
With one() method you retrive just a model and in your $model you have the data you need ..
If you sue all() lie you did you retrive a collection of models and for accessing a single model you must set a proper index eg:
$my_model = $model[0];
In UrlManager config you propably have:
'<controller>/<action>/<id:\d+>' => '<controller>/<action>',
It means default rewrite rules use id as param and it has to be digit, so you can't use controller/view/link. Just to be sure, change action name from actionView to actionTest and then call URL controller/test?link=linklink.
Other solution is to use URL like this: controller/view?link=linklink
The problem is in your url
case 1)
http://localhost/projectName/backend/web/controllerName/actionName/username/rushil
Gives output : Not Found (#404)
case 2)
http://localhost/projectName/backend/web/controllerName/actionName/rushil
Gives output : Not Found (#404)
case 3)
http://localhost/projectName/backend/web/controllerName/actionName?username=rushil
this will work.
Solution : check your url and pass link parameter in url as shown in case 3
404 means your action was not found by url manager. link parameter must be in the url (http://yourapp/controller/view?link=something) because you definded it as actionView($link)
Requesting http://yourapp/controller/view will give you 404 error.
For anyone who may encounter such an error, the problem is after using pretty urls, you need to add rules to map the url format.
C:\xampp\htdocs\your_project\frontend\config\main.php under urlManager add the rules like so:
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => 'news/view',
'<controller:\w+>/<link>' => 'news/latestnews', // This is required for $link parameter
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>/<link>' => 'news/latestnews',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
],
I'm using CakePHP 3 and I want to paginate my users.
However when I click on the second page, the URL looks like /users?page=2 and I expect : /users/2.
I created this route in routes.php :
$routes->connect('/users/:page', ['controller' => 'users', 'action' => 'index'], ['page' => '[0-9]+']);
And in Users/index.ctp before the "prev" button I put :
<?php
$this->Paginator->options([
'url' => [
'controller' => 'users',
'action' => 'index'
]
]);
?>
Now when I click on page 2 for example, /users/2 opens and I got this error message (RuntimeException) :
Unable to locate an object compatible with paginate.
Did I miss something or where I made a mistake ?
Thanks for your help.
The PaginatorHelper has built in the url format, i.e. to use ?page=n. It will also do sorting such as users?page=2&sort=user_id&direction=asc. Your format of /users/{page} does not handle sorting.
If your REALLY want to stick to /users/{page} you'll have to override PaginatorHelper.
try this
in side your controller with paginator component . It works for me
$this->Paginator->paginate('Users')
for custom urlenter code here
u need to implement index action as
public function index($page = null){
$this->Paginator->settings = ['limit' => 15, 'page' => $page];
$this->set('users', $this->Paginator->paginate('Users'));
}
I have a problem with Yii2's urlManager. I have an action with url category/index, where I pass ?par=test as param.
I want to create an alias for my url so that when par is not specified the url will be /test, but when it is specified the url should be /test/some-value. Here is my config for now:
'rules' => [
[
'pattern' => 'test',
'route' => 'category/index',
],
'<subcats: (val|some-value)>' => 'test/<subcats>',
If you need url like category/index/test/some-value. Use that
'category/index/test/<val:\w+>' => 'category/index'
In controller in method index use that:
public function actionIndex($val){
Yii2 automatically provide parameter $val in action.
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
...