Route conflict with public folder in Laravel 4.1.31 - php

I have a route in my app/routes.php i.e.
Route::get('sitemap', [
'as' => 'sitemap',
'uses' => 'PageController#sitemap'
]);
And sitemap.xml in public/ folder.
When I hit the URL http://example.com/sitemap it returns the content written in public/sitemap.xml rather than executing a specified route from app/routes.php
Although, http://example.com/index.php/sitemap works fine.
I need the standard solution for this.

Related

Laravel add dynamic parameter before route

I have a route like
http://localhost.fbleads.com/{dynamic-value}/auth/facebook-login
It give me error that route not found. Any solution for this
In your routes you could maybe have something like:
Route::get('{dynamic-value}/auth/facebook-login', [
'as' => 'auth.login',
'uses' => 'AuthController#login'
]);
the {dynamic_value} can be anything and be accessible as parameter in your controller method

get route in laravel overriding resource route , How to overcome this

I have the following route in my laravel route file:
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
The problem with the above route is , its overriding the below route:
Route::resource('admin', 'adminController');
I want to keep my resource route, but how do i keep my resource ? is there a way around this ??
Modify your route file like this.
Route::resource('admin', 'adminController');
Route::get('/{id}' , [ 'uses' => 'PagesController#show', 'as' => 'getArticle' ]);
Route files executed in the order they are defined.
If you define Route::get('/{id}',.... in the beginning and set your url like http://your-site/admin, the admin section will be considers as the id for the Route::get('/{id}',.... route. So you need to keep it in your mind when you define your route.
just move this route in the end of the web.php file.
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
There are two options:
move Route::get('/{id}', ...) after Route::resource(...)
or add a pattern to Route::get() if id is numeric Route::get('/{id}', ...)->where('id', '[0-9]+');

Laravel routes error get request

Setting up a simple get request seems to throw a http error, this is my code so far:
Link:
<a href="{{ route('account.single', ['id' => $account->id]) }}">
Route:
Route::get('/admin/overview/{id}', [
'uses' => '\App\Http\Controllers\AdminController#getSingle',
'as' => 'account.single',
'middleware' => ['auth', 'admin'],
]);
Shows in the url as - admin/overview?id=5
Controller:
public function getSingle($id)
{
return view('admin.account');
}
This is the error I get - notFoundHttpException in RouteCollection.php line 161:
Note if I remove the /overview/ from the route url so it displays as /admin/{id} it works but the get request doesn't find the id it finds the /overview/ from the url
As mention in Jonathon's comment, the url for your route really should be being displayed as /admin/overview/5 as you've intended and not /admin/overview?id=5.
To identify the problem you'll need to:
Double check you're not seeing cached routes - in the command line run: php artisan route:clear
Check through your routes to make sure you're not hitting another route further up your routes.php file e.g. admin/{some_variable}. You can use php artisan route:list to view the list of routes Laravel is looking for
If you find another route that is causing an issue adjust the order in your routes.php file placing routes with greater specificity above routes with less specificity (example below).
Route::get('/admin/overview/{id}', [
'uses' => '\App\Http\Controllers\AdminController#getSingle',
'as' => 'account.single',
'middleware' => ['auth', 'admin'],
]);
Route::get('/admin/{some_variable}', [
'uses' => '\App\Http\Controllers\AdminController#getSomething',
'as' => 'account.something',
'middleware' => ['auth', 'admin'],
]);
Yours url is incorrect. You should build url like this:
/admin/overview/2
To do it use laravel helper function:
route('account.single', ['id' => 2])

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 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