I have got a Laravel website a: www.website.com and b: www.website.co.kr.
As you can see website b has a different extension 'co.kr'.
Both urls route to the same server. Most pages are the same for both websites, but some pages are different.
I want to handle this in my routing.
So for example:
www.website.com/about-us
www.website.co.kr/about-us
Shows a different page.
Right now I handle this using a group:
$co_kr_routes = function () {
Route::get('/about-us', [
'uses' => 'Frontend\Korea\PagesController#getAboutUs',
]);
};
Route::group(['domain' => 'www.website.co.kr'], $co_kr_routes);
//Default
Route::get('/about-us', [
'uses' => 'Frontend\PagesController#getAboutUs',
]);
This works, but I am not satisfied with the solution and it is causing me not being able to cache the routes 'php artisan route:cache' because of the duplicate route '/about-us'.
My question is:
Is there a nicer way to handle different extensions but same server resulting in showing a different page?
I didn't find much help googling the issue.
Right now I have to very specifically give the .co.kr domain to the group but it would be nicer if I could filter on 'co.kr' instead of the full domain.
I thought a solution would be handling this in the controller method itself, but handling this using routes sounds like the way to go. I dont think the controller should know or care if a request was made from a .com or .co.kr extension.
Related
In route.php I defined a route to a controller with 2 tokens on it.
Route::get('/{category}/{slug}', 'projectController#detail');
Everything is working fine till when there is a call to a URL that have the same structure but has nothing to do with the one that has to be caught by that route shown below.
So when I have for example "/admin/tags", the controller below is triggered because it has the same structure of "/{category}/{slug}" and of course it gives me an error, because it doesn't find a variable.
So now I fixed the problem moving that route on the bottom, but I believe I have to do something to prevent this behavior in advance, cause if I have multiple routes with different tokens everything would be triggered every time and there would be a mess.
So, what is it supposed to do in these cases?
P.S. I'm super beginner with Laravel
use some constraint to the route, reference parameters-regular-expression-constraints. For example:
Route::get('user/{name}', function ($name) {
//
})
->where('name', '[A-Za-z]+');
Or you can make the most specific before unspecific one. For example, in this sequence:
Route::get("/admin/tags", '......');
Route::get('/{category}/{slug}', 'projectController#detail');
if route need two token like that, i'm usually add prefix so my routes looks like this
Route::get('/categories/{category}/slug/{slug}', 'ProjectController#detail');
or
Route::get('/categories/{category}/{slug}', 'ProjectController#detail');
I was having the same issue.
I have constraints on every path parameter (as you always should) and unfortunately the conflict occurs between the following:
Route::get('{userId}/{path}', [
'as' => 'products',
'uses' => 'HomeController#click'
])->where(['id' => '[0-9]+', 'path' => '[0-9a-fA-F]+']);
Route::get('link/{link_path}', [
'as' => 'product-link',
'uses' => 'UserController#productLink'
])->where(['link_path' => '[0-9a-fA-F]+']);
Where even though the one path has the prepended 'link/' in the path it still tried to hit the other. By placing the route with the prepended 'link/' above the other route it took priority and works.
Personally I think if you have a condition that isn't met on the route where clause it should skip the route and move on to the next. It doesn't really make sense to me to put a conditional that doesn't actually get passed up if the conditions aren't met.
Hopefully this helps anyone else having this issue.
My code is below in Routes.php
Route::group([
'middleware' => 'auth',
], function() {
Route::get('/Categories-List', 'Category_Controller#index');
Route::get('/Create-Category', 'Category_Controller#create');
Route::post('/SaveCategory', 'Category_Controller#store')->middleware(['isAdmin']);
Route::post('/UpdateCategory', 'Category_Controller#update')->middleware(['isAdmin']);
});
What's the problem ?
There are still other 100s of routes defined which contains many belongs to admin.
Is there any clean way to isolate the admin routes ?
You can nest route groups:
Route::group([
'middleware' => 'auth',
], function() {
Route::get('/Categories-List', 'Category_Controller#index');
Route::get('/Create-Category', 'Category_Controller#create');
Route::group([
'middleware' => 'isAdmin',
], function() {
Route::post('/SaveCategory', 'Category_Controller#store');
Route::post('/UpdateCategory', 'Category_Controller#update');
});
});
You could also put the admin routes in an entirely separate file via app/Providers/RouteServiceProvider.php (add another line like the existing require app_path('Http/routes.php');).
I bumped into this last week and starred it on github. You can use this Laravel package (Laravel-context) to separate your admin context all together.
Let's say you have 2 contexts in your application: an Administration Panel and a RESTful WebService. This are certainly two completely different contexts as in one context you'll maybe want to get all resources (i.e. including trashed) and in the other one you want only the active ones.
This is when Service Providers come in really handy, the only problem is that Laravel doesn't come with an out of the box solution for loading different Service Providers for different contexts.
This package gives you the possibility to register your different repositories to a single interface and bind them through your Context's Service Provider, using Laravel's amazing IoC Container to resolve which concrete implementation we need to bind depending on which context we are on.
Thanks,
Karmendra
My laravel application has a model - Video. It is the main model so the route was named videos. But after the development I discovered that there is a folder on the production server named videos
So now rewriting the url to include index.php in .htaccess does not work.
I cannot change the name of videos folder which is already present.
I cannot change the db table name either. I don't want to do that, its too much work.
Is there a way to change the route name to something else like lvideos or vvideos?
I tried changing it in routes but it seems there are other places where I have to change it. It throws me an error in the controller.
Can anyone suggest a solution for this?
I don't want to give the link with index.php to the users
Thank you.
You will have to change the route anywhere it is referenced.
In the future if you think a route might change, you could use named routes and then reference the route name anywhere you need to use it.
For example:
Route::group(['prefix' => 'videos'], function() {
Route::get('/', [
'uses' => 'VideosController#index',
'as' => 'videos.index',
]);
Route::get('{id}', [
'uses' => 'VideosController#show',
'as' => 'videos.show',
]);
});
Then everywhere you use these routes you use the name, for example in a view:
Videos
The link will still work even if you change the route to Route::group(['prefix => 'iVideos']); Even though the route changed, the name did not.
I have the following Route:
Route::resource('projects.deliveries.tasks', 'TaskController');
And of course if I want to create a task, my URL looks like this:
http:://test.dev/projects/1/deliveries/3/tasks/create
For Project Number 1 and delivery number 3.
http://i.imgur.com/RlHHY31.jpg
But I don't want the number to show up in the URL, because tasks should be creatable, without authentication or login.
Is there a way to hide these numbers, so that I get a clean URL like this:
http:://test.dev/projects/delivereis/tasks/create
And Laravel understands from my logic, that it is Project 1 and devivery 3 for which a task is to be created?
If you want to do this, you should probably specify your routes manually. Using Route::resource() is great if you're absolutely going to use the resource as intended (with verbose routes) but it doesn't provide you with much flexibility. In fact for most projects it's actually recommended that you do define all your routes manually to give you the most control (and it's also great self-documentation in your routes.php file).
Route::get('projects/deliveries/tasks/create', ['as' => 'projects.deliveries.tasks.create', 'uses' => 'TasksController#create']);
Route::post('projects/deliveries/tasks', ['as' => 'projects.deliveries.tasks.store', 'uses' => 'TasksController#store']);
I'm trying to understand routing in Laravel 4. I read a good post here on StackOverflow and a link to beware the route to evil, a post about manually specifying routes. I like the idea of specifying my routes manually and having the routes.php act as documentation. But it seems like I need to be cautious about the order of my Routes if I'm going to specify my own instead of using Route::resource() If I have the new or create route before the show then I won't be routed to the show because of the variable in URI? The order in which the routes are defined is important right?
// This will not work if I try and browse to dogs/new
Route::get('dogs', array('as' => 'dogs', 'uses' => 'DogsController#index'));
Route::get('dogs/{dogs}', array('as' => 'dog', 'uses' => 'DogsController#show'));
Route::get('dogs/new', array('as' => 'new_dog', 'uses' => 'DogsController#create'));
It seems I need to make sure that the dogs/new comes before the dogs/{dogs} for new to return correctly. I'm not clear on what {dogs} does or that's different from (:any) or {any} I've seen a few different uses in examples and pseudo code. I see that /new is the same as {...} when the route is before the more specific is the {} like a wildcard in Laravel 4? Is the (:...) the old way?
As an aside I've noticed a different naming convention from some of the examples I've seen when I run php artisan routes with a resource route like Route::resource('photos', 'PhotosController'); The method and named route for post to index to a create a new resource is named photos.store and #store. The method and named route for a link to a form to create a new resource is photos.create and #create. Is that Laravel 4 thing or conventions in other frameworks?
Route::get('dogs/{dogs}', array('as' => 'dog', 'uses' => 'DogsController#show'));
The above url expecting a parameter after dogs segment.
for example: http://laravel.com/dogs/xyz, http://laravel.com/dogs/new
after dogs url segment, Laravel will accept anything. So, your another routing will never executed for the route parameter.
Route::get('dogs/new', array('as' => 'new_dog', 'uses' => 'DogsController#create'));
More about route parameters:
http://laravel.com/docs/routing#route-parameters
Resource Controllers
Laravel and Ruby on rails support resource full routing. I think, Tailor borrow the resource full routing idea from Ruby on rails.
The following routes will generate if you use resource controller:
index
create
store
update
show
edit
destroy
http://guides.rubyonrails.org/routing.html
http://laravel.com/docs/controllers#resource-controllers