Laravel router redirect to wrong path - laravel

I wrote this code expecting to be redirected to the root url (/ or /home), but instead, it's redirecting to %7Bhome%7D.
Route::get('/{home}', ['as' => 'home', function () {
return view('home');
}])->where('home', '(home)?');
Does anyone know where the problem is?
note: it does support both urls (/ or /home). the problem is just when calling for its name on redirects.
Thanks in advance!

One solution is just to create two routes with the two URLs:
Route::get('/', 'yourController#methodName');
and the other one:
Route::get('/home', 'yourController#methodName');
As you might know, the %7B and %7D are referring to the left and right curly braces { }. I'm not sure why, but it's obviously not leaving out the curly braces when it tries to direct to your route .

It's strange that You cannot redirect using named routes.
Give a try to this:
Route::get('{location}', ['as' => 'home', function ($location = 'home') {
return view($location);
}])->where('location', '(home)?');
or how about this:
Route::any('/', ['as' => 'home', function () {
return view('home');
}]);

Related

preg_match(): Compilation failed: invalid range in character class at offset 3

My laravel Project Route invalid range in character class prolem please help me?
Route::get('{path}','HomeController#index')->where( 'path', '([A-z]+)?' )
Not solve
eRoute::group(['namespace' => 'Post'], function ($router) {
$router->pattern('id', '[0-9]+');
// $router->pattern('slug', '.*');
$router->pattern('slug', '^(?=.*)((?!\/).)*$');
// SingleStep Post creation
Route::group(['namespace' => 'CreateOrEdit\SingleStep'], function ($router) {
Route::get('create', 'CreateController#getForm');
Route::post('create', 'CreateController#postForm');
Route::get('create/finish', 'CreateController#finish');
I don't know what the intended routing pattern is supposed to be, but I speculate that it does not supports lookarounds. So instead of this:
$router->pattern('slug', '^(?=.*)((?!\/).)*$')
Try this:
$router->pattern('slug', '^[^/]*$');

why i see Call to undefined method Error?

im learning laravel so if im not well as you are accept my apologies...
my problem is when i try to define a new method in web.php i got error!some times phpstorm sets problem on 'Route'word so i can run my blade pages but sometimes it sets problem on 'get','post','group' ,...and i cant run my app
ill show you how i defined my routes
use Illuminate\Routing\Route;
Route::get('/', function () {
return view('welcome');
});
Route::group(['prefix' => 'admin'] ,function (){
Route::get('/users','UsersController#index');
});
after all this my point is making a controller so i got this error to fix so i can move on...
Named groups in laravel
Route::group(['prefix'=>'admins','as'=>'admin.'], function(){
Route::get('users', ['as' => 'user', 'uses' = > 'UsersController#index']);
});
Also make sure you have index method in your UsersController.
FYR :- https://laraveldaily.com/laravel-5-1-names-for-route-groups/

How to add dynamically prefix to routes?

In session i set default language code for example de. And now i want that in link i have something like this: www.something.com/de/something.
Problem is that i cant access session in routes. Any suggestion how can i do this?
$langs = Languages::getLangCode();
if (in_array($lang, $langs)) {
Session::put('locale', $lang);
return redirect::back();
}
return;
Route::get('blog/articles', 'StandardUser\UserBlogController#AllArticles');
So i need to pass to route as prefix this locale session.
If you want to generate a link to your routes with the code of the current language, then you need to create routes group with a dynamic prefix like this:
Example in Laravel 5.7:
Route::prefix(app()->getLocale())->group(function () {
Route::get('/', function () {
return route('index');
})->name('index');
Route::get('/post/{id}', function ($id) {
return route('post', ['id' => $id]);
})->name('post');
});
When you use named routes, URLs to route with current language code will be automatically generated.
Example links:
http://website.com/en/
http://website.com/en/post/16
Note: Instead of laravel app()->getLocale() method you can use your own Languages::getLangCode() method.
If you have more questions about this topic then let me know about it.
Maybe
Route::group([
'prefix' => Languages::getLangCode()
], function () {
Route::get('/', ['as' => 'main', 'uses' => 'IndexController#index']);
});

How to fix Route not defined error in view, with Laravel?

I am getting an errorexception on my view(ErrorException in UrlGenerator.php line 296:). This is my view code:
<?php echo basename($file['name']);?>
For a while it worked, but not don't and I don't know why and how to fix it. I guess I must write something in route.php, but not sure what.....
Route::get('home', ['as' => 'home', function () {
return 'home';
}
]);

Laravel 4 : Default route for route group

I try to handle the default route of a route group, I have this but it doesn't work.
Route::group(array('prefix' => 'administrator'), function() {
Route::get('/', 'AdminUserController#getLogin');
Route::controller('page', 'AdminPageController');
Route::controller('user', 'AdminUserController');
Route::controller('menu', 'AdminMenuController');
});
Does anyone know how to do that ?
Thank you
Just figured out. You are missing a uses, and the second parameter should be an array.
Route::get('/', ['uses' => 'AdminUserController#getLogin']);

Resources