Laravel route to same depth - php

I have a site in a subfolder, example.com/mysite. I have a route there, login, which is accessed via example.com/mysite/login. This is the route:
Route::group(array('domain' => 'example.com'), function()
{
Route::get('/', function()
{
return Redirect::to('login');
});
Route::get('/login/', array('as' => 'login', 'uses' => 'AccountController#login'));
})
The problem is, when I have a link in my view like so:
<a href='{{{ route('login') }}}'>Login page</a>
It displays the path example.com/login instead of example.com/mysite/login. It think this has something to do with the domain group, but I don't know how to tell it to keep the current subdirectory. I've tried changing the domain to example.com/mysite in the group command, but that just causes an error. The redirection to login works, but how do I get the correct link in the view?

Turns out, this is because when it generates the route, it uses the domain provided in the group instead of getting it from the url. It can be overriden though, by specifying an empty domain in the individual route match, like so:
Route::get('/login/', array('as' => 'login', 'uses' => 'AccountController#login', 'domain' => ''));
This will allow it to ignore the domain specified in the group function. Unfortunately, from what I can tell, you need to do this for all of them.

Your routes do not follow a relative path. Even if you are in example.com/mysite, your route is made within the example.com domain.
Your redirect redirects you to example.com/login.
This is where your route is. Your redirect is redirecting to 'login'.
You named your route vendor_login. Therefore within the view, you have to write <a href='{{{ route('vendor_login') }}}'> to access your correct route.
Still, going to example.com/mysite/login will not show you your login page. It is available via example.com/login.
If you want to be available via example.com/mysite/login, use the following code.
Route::group(array('domain' => 'example.com'), function()
{
Route::get('/', function()
{
return Redirect::route('vendor_login');
});
Route::group(array('prefix' => 'mysite'), function()
{
Route::get('/login/', array('as' => 'vendor_login', 'uses' => 'AccountController#login'));
});
});
The difference here is that the login route is now placed within a group that uses the 'mysite' prefix. In this case, your login route is available via example.com/mysite/login.
The redirect is changed from Redirect::to('login') (which redirected to example.com/login, to Redirect::route('vendor_login') which redirects to the named route vendor_login, independent of its path.
EDIT:
Routes are absolute. If you want to make a link that is relative to the same depth, in your view just do Login. But this is not recommended. In that case you can not show the same view/link from routes with a different depth. Instead use named routes (as you did) which give you the most flexibility. Just put in your view the correct name vendor_login and change your Redirect::to to Redirect::route('vendor_login')
Hope this helps

Related

Laravel 5.1 - Overloaded routes

I have a homegrown, Laravel 5.1 base application on top of which I build specific applications. The base application uses a named route for login, naturally called "login", which listens for GET /login.
In one of my specific applications, I attempted to overload that route to send the requests to a different controller. It seemed to work for a while, but then it started going to the base application's controller again. I'm sure I changed something to break it, but the problem is that I can't figure out how to fix it again.
My base application routes are all defined in app/Http/Routes/core.php. The relevant route:
Route::get('login', [
'as' => 'login',
'uses' => '\MyVendor\Core\Http\Controllers\AuthController#getLogin'
]);
My specific application routes are defined in app/Http/Routes/app1.php. The relevant route:
Route::get('login', [
'as' => 'login',
'uses' => 'App1\AuthController#getLogin'
]);
App2 and App3 are defined similarly. My app/Http/routes.php adds these routes like this:
require 'Routes/core.php';
Route::group(['domain' => 'app1.com'], function() {
require 'Routes/app1.php';
});
Route::group(['domain' => 'app2.com', function() {
require 'Routes/app2.php';
});
Route::group(['domain' => 'app3.com', function() {
require 'Routes/app3.php';
});
The problem I am seeing is that visiting app1.com/login, app2.com/login, and app3.com/login all result in the execution of \MyVendor\Core\Http\Controllers\AuthController#getLogin rather than App1\AuthController#getLogin.
EDIT: I have changed the problem description since I was describing it incorrectly as a problem with calls to route('login').
The index of the routes in Laravel follows a "$domain$uri" format, therefore routes with a domain won't overwrite those without. A fallback route without a domain should be declared after the domain group, so it is later in the route collection and won't match before a route with a matching domain.
"the most recent definition for a route is the effective route"
This is not a bug, this is the expected behaviour, a simple example would be setting a variable to value 1 then setting it to value 2, of course the (most) recent value takes place.

Laravel - Can the route and model have different names

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.

Laravel Subdirectory Views

what I'm trying to do is set it up so that the user can go to "/project/index" ("/" being the route ofc) but I'm not quite sure how to do it in laravel?
What I currently have:
Routing:
Route::get('project.index', array('as' => 'project/index', 'uses' => 'ProjectController#indexPage'));
Also in routing:
View::addLocation('project'); //Project View
View::addNamespace('project', 'project');
In my Project Controller:
public function indexPage()
{
return View::make('index', array('pageTitle' => 'Project Index'));
}
Any ideas? Thanks in advance.
PS: It's Laravel 4
You have your routing a little wrong. Try out the following
Route::get('project/index', ['as' => 'project.index', 'uses' => 'ProjectController#index']);
So the first parameter into the Route::get() function should be the URL the user is visiting for example http://example.com/project/index. The as key in the array provided is the name you're giving to the route.
By giving the route a name you can use this throughout your application, rather than using the url the user is visiting. For example you might want to generate a link to your route
Link
This will generate a link to http://example.com/project/index. This makes it convenient in the future should you wish to change your URLs without changing lots of links throughout your view files.
Route::get('foobar/index', ['as' => 'project.index', 'uses' => 'ProjectController#index']);
The URL generated through route('project/index') would now be http://example.com/foobar/index
Checkout the routing documentation for further information http://laravel.com/docs/4.2/routing

Redirect to correct location in wildcard prefix in route group using Laravel

I have got a prefix of location before a lot of my urls, for example example.com/london/
The problem is that when I want to redirect in my controller using the alias, like so:
if($validator->fails()) {
return Redirect::route('register')->withErrors($validator);
}
It redirects to example.com/%7Blocation%7D/register instead of example.com/london/register
Is there an easy fix for this so that it contains the correct location, or will I have to manually put in the location every time I redirect?
My routes.php
Route::group(['prefix' => '{location}'], function() {
Route::get('/', 'LocationController#home');
Route::get('/register', array('as' => 'register', 'uses' => 'AuthController#getRegister'))->before('guest');
Route::post('/register', array('uses' => 'AuthController#postRegister'))->before('csrf');
})
{location} is handled like a normal route parameter, so you can just pass it as second argument:
return Redirect::route('register', 'london')->withErrors($validator);
Since it is a route parameter you can also retrieve it like one. With Route::input(). That means if you want to redirect to a route with same prefix as the current:
return Redirect::route('register', Route::input('location'))->withErrors($validator);
You might also add a default value: Route::input('location', 'london')

Laravel 4 - route is not defined, on redirect

I'm trying to setup a simple redirect after a login.
The logging in part works but the redirect fails because it says the route doesn't exist.
This is my routes file:
Route::any('/', array('uses' => 'UsersController#login'));
Route::any('/manage', array('uses' => 'AdminController#showWelcome'));
And the route works fine if i go to http://example.com/manage .. the logo of laravel is there, and my other page is fine as well.
But when i do:
Redirect::route('/manage');
the page dies saying:
Route [/manage] not defined
Anybody have an idea?
You should use the route name when you are using Redirect::route method and in this case you have to declare the route using a name, i.e.
Route::any('/manage', array('as' => 'manage', 'uses' => 'AdminController#showWelcome'));
Here, as value is name of the route, so, now you can use
return Redirect::route('manage'); // 'manage' is the name of the route to redirect
Or, alternatively, you can use Redirect::to('url') method, i.e.
return Redirect::to('/manage'); // '/manage' is the url to redirect
Check Redirect to a named Route and named routes.
This error "Route [manage] not defined" is because of the route name "manage" is not defined.
Route name and Route path are two different things.
And you've declared the route path as admin,
Route::any('manage', 'AdminController#showWelcome');
However,
return redirect()->route('manage');
means that you are redirecting the flow to the route named "manage".
To sort the error,
Define a route name "manage" as follows in an array defined below with 'as' => 'route_name'.
Solution :
Route::any('manage', [
'as' => 'manage',
'uses' => 'AdminController#showWelcome'
]);
Please refer the link : https://laravel.com/docs/master/routing#named-routes
use return Redirect::intended('mannage');

Categories