Laravel Route Group Prefixing and Breadcrumbs - php

I am using route prefixing and I am coming up with an issue and not sure how to fix it.
First actual question is can I keep my links the same as they were before I added the prefixing?
Previously I had links for example and my routes were like this.
Edit
Route::resource('users', 'UsersController');
However now I have added in a prefix to a group of routes so that links will show up as mysite.com/backstage/users.
Route::group(array('prefix' => 'backstage'), function()
{
Route::resource('users', 'UsersController');
});
So now with ALL my views do I have to go through each one and edit all the links so that they look like this or can I do something different so I can handle this situation differently.
Edit
I'm also concerned about my breadcrumbs for my application.
<?php
Breadcrumbs::register('users.index', function($breadcrumbs) {
$breadcrumbs->parent('home');
$breadcrumbs->push('Users', route('backstage.users.index'));
});
Do I have to include the backstage or can I keep it without?

The only way I know to do it is pass names as a third argument like:
Route::resource('users', 'UsersController',
['names'=>['index'=>'users', 'store'=>'users.store']]);
A lot of people avoid using the resource routing. Phil explains some reasoning here Beware the route to evil. Basically he's saying it can be a nightmare if you, or someone else, comes back to it later to figure out. You often have special cases that you put ahead of the resource so they get hit first in the routing and the whole thing turns into a mess.

Related

Laravel Routes Url issue

Hello Everyone I am posting this question because I don't find this anywhere so please try to provide solution for this problem.
Route::get('/product/{Cat_Slug}/{slug}','ProductController#SingleProductShow')>name('SingleProduct.Show');
This is my route and i want to remove 'product' from url and make my url like. www.example.com/productCat_Slug/ProductSubCat_Slug and want to access all related products but when i do this other routes effected.
Make sure your other routes (those affected by your changes) come before this route in routes/web.php, so they are "hit" first.
Route::get('/test', 'TestController#test');
Route::get('/{user}', 'TestController#user');
instead of
Route::get('/{user}', 'TestController#user');
Route::get('/test', 'TestController#test');
Try this
Route::get('/{Cat_Slug}/{Subcat_slug}','ProductController#SingleProductShow')->name('SingleProduct.Show');
After that you will be able to access the values contained in Cat_Slug and Catsub_slug in your controller and make the query you want.
You also have a syntax error close to the route name. use ->name('myRouteName') instead of >name('myRouteName')

Laravel - why not relative URL's?

I have been learning Laravel recently and I have seemingly missed a key point: why should relative links be avoided?
For example, I have been suggested to use URL::to() which outputs the full path of the page passed as a parameter - but why do this when you could just insert a relative link anyway? E.g., putting URL::to('my/page') into a <href> will just insert http://www.mywebsite.com/my/page into the <href>; but on my website href='my/page' works exactly the same. On my website I have based all relative URL's from the index.php file found in the public directory.
Clearly, I'm missing a key point as to why full paths are used.
I've found that using route() on named routes to be a much better practice. If at one point you decide, for example, that your admin panel shouldn't point to example.com/admin, but example.com/dashboard, you will have to sift through your entire code to find all references to Url::to("/admin"). With named routes, you just have to change the reference in routes.php
Example:
Route::get('/dashboard', ['as' => 'admin', 'uses' => 'AdminController#index']);
Now every time you need to provide a link to your admin page, just do this:
Admin
Much better approach, in my opinion.
This is even available in your backend, say in AdminController.php
// do stuff
return redirect()->route('admin');
http://laravel.com/docs/5.1/routing#named-routes
Neither absolute nor relative links should be used - it's advisable to use named routes like so:
Route::get('my/page', ['as' => 'myPage', function () {
// return something
}]);
or
Route::get('my/page', 'FooController#showPage')->name('myPage');
Then, generate links to pages using URL::route() method (aliased as route() in L5), which is available in Blade as well as in your backend code.
That way, you can change the path to your routes at any time without having to worry about breaking links anywhere in your application.

Laravel - Change base URL globally?

Let assume, the base URL of my application is - http://www.example.com (I have not set anything in any config file to specify this). There are a lot of hard coded urls in the application.
eg. Contact
Now, if I go though the application using an URL like - http://www.example.com/country, is it possible to assign a global base URL, where when I click on contact it will take me to - http://www.example.com/country/contact.
There are a lot of such hard-coded URL, changing it individually will take a lot of time (like appending it with a global variable). Is there any simpler way to do this or is there any config specific for this in laravel? I am fairly new to laravel. Any help would be appreciated.
You can use the somewhat cumbersome solution of applying a filter, as suggested by #worldask, but I think it would be better to set a named route and change all occurrences using a regular expression (any decent editor allows for that). That way, for the lifetime of your application you only need to change the routes in routes.php, and it will be reflected everywhere.
e.g
Route::get('country/contact', ['as' => 'contact',
'uses' => 'SomeController#someFn'];
Contact
Of course, the same principle applies to adding a prefixed group of routes, so you can wrap the entire routes file with a group prefixed by 'country'.
you can try Route Prefixing
Route::group(array('prefix' => 'country'), function(){
Route::get('Contact', 'HomeController#index');
Route::get('another', 'HomeController#index');
});
edit
try route filter
Route::filter('filtername', function($route, $request, $value)
{
if ($route == 'country') {
return Redirect::to(url);
}
});

Handling generated urls in laravel?

I'm making a link shortener as part of a school project,
Route::get('{short_url}', array('uses' => 'UrlController#shortUrlRedirect');
This function works fine alone, but as I have other functions such as
Route::post('register', array('uses' => 'HomeController#doRegister'));
whenever example.com/anylink
is now used, it is handled by one function alone.
A working solution I found would be to do something like:
Route::get('url/{short_url}', array('uses' => 'UrlController#shortUrlRedirect');
But of course with a link shortener, the goal is to have as little characters as possible.
Any ideas of a possible way to handle this issue within laravel?
The earlier or 'higher' in the routes.php file is the route, the more priority it gets, so if you define two identical routes or two routes that match one pattern, like in your example, the first one will be executed.
So you should define register route earlier, as it should not be overriden by the {short_url}.
Here is the explanation: Routes: First in, first out
TL;DR: Laravel receives a request, and uses the URI of the request to find a matching pattern iterating the routes file, when it finds one, it break;s the loop.

Change Laravel's default (root) controller

In Laravel the default controller is the Home_Controller. However I have a controller called frontend. I want to use this instead of the home controller.
When I register a route like this:
Route::controller(Controller::detect());
then a request to /offer will be handled from within the home controller like home#offer. I want to use frontend#offer and access it from the site's root - not like /frontend/offer.
What should I do?
Thanks in advance.
Home_Controller is one of the hard-coded convention which exist in Laravel 3, however there are still ways to define routing to point the Frontend_Controller methods, my preference would be.
Route::any('/(index|offer|something)', function ($action)
{
return Controller::call("frontend#{$action}");
});
Limitation with this is that you need to define all supported "actions" method in Frontend_Controller.
My guess is that the only reason you think the Home_Controller is some sort of default is because you are using Controller::detect(); I really haven't seen anything in the documentation to make me think that the Home_Controller is anything special at all. In fact, it doesn't even look like it is routed to in the example documentation. Given that, my first suggestion would be to get rid of Controller::detect() and see if that fixes your problem.
Barring that, have you tried registering frontend as route named home? It appears that all URL::home() does is search for the 'Home' route, and then redirect to it. When using controller routing this can be done with something to the effect of.
Route::get('/',
array(
'as' => 'home',
'uses' => 'frontend#index'
)
);
Or is that not your desired effect? Do you want all routes which aren't otherwise found to be redirected to your frontend controller?
If you are concerned about your urls looking pretty, you can probably use some rewrite rules in your .htaccess file to make the whole process of routing to /frontend/index transparent you your users.
Add this to your routes.php :
Route::get('/', array('as' => 'any.route.name', 'uses' => 'frontend#offer'));
If you have any other / route, just remove it.

Categories