In a Laravel 5.8 app i want my url's to be in Dutch, however, for consistency and just general easier use, i want to name everything else by their English names. Eloquent will ( as far as i know ) only pass me the proper vars if my Model, Controller, table etc. are all named the same.
Right now, i have a route named /backend/facturen which in English would be /backend/invoices. I have tried using names routes, however, i found that this wasn't what i was looking for.
My route:
Route::resource('/backend/facturen', 'InvoicesController');
My show method inside the InvoicesController:
public function show(Invoice $invoice) {
return view('backend.invoice.show', compact('invoice'));
}
The database table is named 'invoices'.
The only way so far i have got this to work is by renaming my route to:
Route::resource('/backend/invoices', 'InvoicesController');
But, of course, this is not a solution to my problem.
I would like to see all data from the invoices show up on my ( Dutch ) /facturen route.
Resource controllers by default look for a variable with the same name as the (last part of the) URL, so renaming the URL changes that variable as well.
But you can tell Laravel to use a different name:
Route::resource('/backend/facturen', 'InvoicesController')->parameters([
'facturen' => 'invoice'
]);
You could also get the variable itself from the id, without using the laravel magic to correctly detecting the variable from the url:
public function show($id) {
$invoice = Invoice::findOrFail($id);
return view('backend.invoice.show', compact('invoice'));
}
No, you have registered a route pointing to the URL /backend/facturen, to name it, use the name function:
Route::resource('/backend/facturen', 'InvoicesController')->name('backend.invoice.show');
Even then, it won't show that route, when using the view function you need to pass the path to your template, not the route.
Related
I'm currently using laravel 5.4 and I have stumbled upon something I can't fix.
I'm currently trying to bind a route to a controller using the Laravel resource helper as such :
Route::resource('campaigns', 'CampaignsController');.
I correctly see my route being there when I do a PHP artisan:route list, I have all my CRUD endpoints tied to the appropriate controller function. Also, note that I'm currently doing that for all my route that need to be tied to a CRUD system ( what I'm working with is mostly form ) without any problem
With this being said, whenever I'm trying to edit a Campaign, I get an error : Class App\Http\Controllers\Ads\Campaigns does not exist
I do not know why it's trying to look for a Campaigns controller while I specify the CampaignsController controller. Everything is behaving correctly in campaigns route, except the edit one. Also, all my other routes have the same logic and never faced this problem.
Any idea why it is looking for the wrong Controller ?
Here's my namespace declaration and folder hierarchy, which is ok ( please note that the adsController has its routes declared the same way and is used the same way too )
here's my edit method
and here's the error
It's quite possible that you try to inject not existing class in your controller.
Take a look at controller constructor or edit route if you don't have something like this:
public function edit(Campaigns $campaigns)
{
}
and make sure you import Campaigns from valid namespace (probably it's not in App\Http\Controllers\Ads namespace.
If it doesn't help try to find in your app directory occurrences of Ads\Campaigns to see where it's used. Sometimes problem can be in completely different part of your application.
EDIT
Also make sure you didn't make any typo. In error you have Campaigns but your model is probably Campaign - is it possible that in one place you have extra s at the end?
Try with Route::resource('campaigns', 'Ads\CampaignsController'); in your web.php file
We have multiple client portal each one has a unique url like
xyz.com/ClientPotal123
xyz.com/ClientPotal234
xyz.com/ClientPotalXXX
We will be routing all these url's to
/var/www/html/Laravelapp/public
Laravelapp is our codebase which we use for all the clients.
Since ClientPotalXXX is dynamic and unique for all the clients, I need to get the value of ClientPotalXXX for loading client specific settings like url generation, database connection (We have different database for each client).
To achieve above I've done below changes..
My Web.php file is as below..
Route::pattern('ClientPortal','^ClientPortal([0-9]+)?');
Route::prefix('/{ClientPortal}')->group(function () {
Route::get('/user/list', 'UserController#list')->name('list');
Route::get('/user/edit/{id}', 'UserController#edit');
});
I've created Middleware with below code written in it..
public function handle($request, Closure $next)
{
$database_name = strtolower($request->ClientPortal).'_db';
config(['database.connections.mysql.database'=>$database_name]);
config(['app_settings.client'=>$request->ClientPortal]);
return $next($request);
}
And it's working fine but previously I used to access $id in edit function directly
public function edit($id){
echo $id; // 12
}
But now $id return the value of ClientPortalXX everytime.
If I access id from Request it works fine
public function edit(Request $request){
$id = $request->id; // 12
}
This is happening with all of the other routes where I'm using route parameters.
So I'm not sure if this happened because I'm using dynamic prefix for grouping all the routes?
And now for every route() method which I've used in blade files for url generation I have to pass the second parameter ie. Value of {ClientPortal}
{{route('register',['ClientPortal'=>config('app_settings.client')])}}
Is this right implementation? I know we can make any varibale accessible globally using service provider but will it be right to do so?
My Laravel Version is 5.5.xx.. I'm just a beginner so any Help/Suggestion/Advice will be appreciated Thanks :)
Update:
Nikola Gavric and Oluwafemi Sule had already clarified my doubt in comments below.
But since the group prefix is dynamic, How do I handle the route naming case?
If I had to generate user list url using list route name which is mentioned in above web.php file.
Now I've to change this line..
{{ route('list') }}
To
{{ route('list',['ClientPortal'=> 'ClientPortalXXX' ]) }}
Since prefix is also a route param.
Is this feasible option? Because I've to do this change everywhere where I've used route method for url generation.
I am developing an application in the Laravel 5.2 which must have a friendly URL-s. This is not problem with the regular way, where the {slug} wildcard is handled by a controller but I want to make it in a different way.
For now I have only two controllers:
ProductsController#show to show details product
CategoriesController#show to show selected category with listing products which are assigned to it
And routes:
Route::get('product/{product}', 'ProductsCategory#show')->name('product.show');
Route::get('category/{category}', 'CategoriesController#show')->name('category.show');
So when I want to echo a just use route('product.show', compact('product'))
Nothing special until I want to handle different URL-s which are fetched from a database. I thought it will be possible to make an another routes which are assigned to existing and when I use a route(...) helper it will be handled automatically. But it is not. So for example I have a new URL:
domain.com/new-url-for-product.html
so by route it should be assigned to the regular 'product.show' route with some ID, which is handled by route model binder for {product} wildcard. The same for route(..) helper it should print friendly URL-s.
I don't know if my strategy is good. How do you handle with the similar problems?
of course route will handle this automatically. have a look into this. I am just giving you example, you have to set this code as per your need.
route('product.show', 'product'=>'new-url-for-product.html');
will generate this
domain.com/new-url-for-product.html
route('product.show', 'product'=>'new-url2-for-product.html');
will generate this URL
domain.com/new-url2-for-product.html
and so on, and then you have to handle all this in your controller method.
eg: your controller method for this route is ProductsCategory#show which is
public function show($product){
if($product == 'new-url-for-product.html'){
//perform this
}
if($product == 'new-url2-for-product.html'){
//perform this
}
}
this just an example, you have to map this according to your need
Edited Here is the example I tested it
Route::get('/product/{product}.html', ['as'=>'product.show', 'uses'=>'ProductsCategory#show']);
I have a user RESTful resource route in my Laravel App.
Route::resource('backbone.users', 'backbone\UserController');
for the the CRUD operations.
Unfortunately, I get following URIs:
I get {backbone} in the URI that results in %backbone% in the browser but
I want the URL like dev.domain/backbone/users NOT dev.domain/backbone/%backbone%/users
I would need to redirect like:
return Redirect::intended('backbone/{backbone}/users');
How come?
It's hard to tell by the screenshot (or maybe my eyes aren't great) but it looks like backbone/{backbone}/users. If this is the case, that's intended, the {} are indicating that it can take a variable in this position.
So it might be return Redirect::intended('background/1/users');
The controller method to go with it, in this case index of UserController will receive that variable as its argument.
This is normal for Laravel. The {} in your routes list represent url variables.
Take this route for example:
/users/{user}/edit
To use this route you would navigate to
/users/1/edit
Assuming you have your controller method setup similar to:
public function edit($userId)
{
}
The $userId variable would contain '1' from the url. Definitely checkout the docs for more on Laravel routing
I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?