Laravel routing resource error - laravel

i'm using laravel 5.0
I have 2 routing :
Route::get('admin', 'AdminController#index');
Route::resource('admin/user','UserController');
If I browse to http://localhost:8000/admin/user
It works fine
but if I using this :
Route::resource('admin', 'AdminController');
Route::resource('admin/user','UserController');
The page at http://localhost:8000/admin/user will be blank!
Why? and how to fix it ?
Thank you

Follow this order:
Route::get('admin', 'AdminController#index');
Route::resource('admin/user','UserController');
Route::resource('admin', 'AdminController');
It's just the problem with the priority of the routes.
The route, Route::get('admin/{id}', 'AdminController#show') will take precedence over the Route::get('admin/user', 'UsersController#show');

Related

Laravel 9 Route problem return 404 NOT FOUND

I'm trying to render a view but I always get notfoud 404 I don't know how to solve this question anymore if anyone can help me I would be grateful.
any help is welcome.
Thanks in advance.
Everyone have a great Monday.
CommunityPostController -> code ->
public function creategif(Community $community)
{
return view('posts.creategif', compact('community'));
}
Web Routes
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('u/{id}', [App\Http\Controllers\HomeController::class, 'user'])->name('user.view');
Auth::routes(['verify' => true]);
Route::group(['middleware' =>['auth', 'verified']], function (){
Route::resource('communities', \App\Http\Controllers\CommunityController::class);
Route::resource('communities.posts', \App\Http\Controllers\CommunityPostController::class);
Route::resource('posts.comments', \App\Http\Controllers\PostCommentController::class);
Route::get('communities/{community}/posts/creategif', [\App\Http\Controllers\CommunityPostController::class, 'creategif']);
Route::get('posts/{post_id}/vote/{vote}', [\App\Http\Controllers\CommunityPostController::class, 'vote'])->name('post.vote');
});
Views Files Structure
There might be a conflict base on your route order and the image you've posted. As you can see you have resource for your route which in this case this conflict might happen base on route order, and your laravel is actually trying to get a post instead of loading creategif route.
Which in this case every time you try to get access to creategif route, your application is actually trying to load a post base on 'communities.posts' route.
So base on this conflict in your route order, this is working fine and create or any other routes related to 'communities.posts' should work fine, but in other-hand creategif in URL might recognized as a route related to 'communities.posts' resource.
Move your route to top, or in this case just above 'communities.posts' route, don't forget to clear route cache.
Route::group(['middleware' =>['auth', 'verified']], function (){
Route::get('communities/{community}/posts/creategif', [\App\Http\Controllers\CommunityPostController::class, 'creategif']);
Route::resource('communities', \App\Http\Controllers\CommunityController::class);
Route::resource('communities.posts', \App\Http\Controllers\CommunityPostController::class);
Route::resource('posts.comments', \App\Http\Controllers\PostCommentController::class);
Route::get('posts/{post_id}/vote/{vote}', [\App\Http\Controllers\CommunityPostController::class, 'vote'])->name('post.vote');
});

Laravel event dispatch in web.php

I just wanted to use event broadcasting in Laravel and I followed a tutorial video. But problem shows up in the beginning. The tutorial shows what to do in web.php and it goes like this:
Route::get( uri: '/'. action: function () {
eventName::dispatch();
return view( view: 'welcome');
});
The problem is I have a different route formula in my web.php which is like this:
Route::get('{path}', SpaController::class)->where('path', '(.*)');
The question is, how to dispatch the event in my web.php?
Solve based on #ceejayoz's comment:
The tutorial is showing a quick, no-controller approach. You would call
eventName::dispatch(); within your SpaController.

route laravel not defined

i have this route:
Route::get('blog/search', 'web\BlogController#localSearch')->name($this->prefix.'blogSingle.localSearch');
this route it´s inside group:
Route::group(['prefix' => $locale, 'where' => ['locale' => '[a-zA-Z]{2}']], function(){
//Route::get('/', 'Web\HomeController#index')->name($this->prefix.'home.index');
Route::get('/', 'Web\ManagerController#index')->name($this->prefix.'home.index');
Route::get('about', 'Web\AboutController#index')->name($this->prefix.'about.index');
Route::get('contact', 'Web\ContactController#index')->name($this->prefix.'contact.index');
Route::get('help', 'Web\HelpController#index')->name($this->prefix.'help.index');
Route::get('local/{url}', 'Web\LocalController#index')->name($this->prefix.'local.index');
Route::get('privacy-policy', 'Web\PrivacyController#index')->name($this->prefix.'privacy.index');
Route::get('managers', 'Web\ManagerController#index')->name($this->prefix.'manager.index');
Route::get('blog', 'Web\BlogController#index')->name($this->prefix.'blog.index');
Route::get('search', 'Web\SearchController#index')->name($this->prefix.'search.index');
Route::get('suggest-local', 'Web\SuggestController#index')->name($this->prefix.'suggest.index');
Route::get('terms-conditions', 'Web\TermController#index')->name($this->prefix.'term.index');
Route::get('blog/{url}', 'web\BlogController#show')->name($this->prefix.'blogSingle.show');
Route::get('blog/likeit/{id}', 'web\BlogController#likeit')->name($this->prefix.'blogSingle.likeit');
Route::get('blog/search', 'web\BlogController#localSearch')->name($this->prefix.'blogSingle.localSearch');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController#showResetForm')->name($this->prefix.'password.reset');
Route::get('email/verify/{id}', 'Auth\VerificationController#verify')->name($this->prefix.'verification.verify');
});
}
but i need call to action form:
<form action="{{ route('blog/search') }}" method="get" class="fl-wrap" id="searchRestaurant">
and when send my form, returned this:
Route [blog/search] not defined. (View: C:\wamp64\www\guiaPaladar\resources\views\layouts\right_sidebar_blog.blade.php)
this error appear when blade load, so i can´t do anything
i don´t show my wrong, others routes i can use in other form, for example blog/likeit/{id} i used in href to add like in post blog...
i hope that anybody can help me, please. Sorry for my english
Thanks
If you are sure that the route blog/search is registered after using php artisan route:list to check. However, your change is not reflected; You may have to clear cache. Please use the following command:
php artisan route:clear

Laravel 4 - How to redirect without subdomain parameter name in url

I need some assistance clarifying how to properly generate redirects in laravel when the subdomain of the url is being used as a parameter in the routing.
The route in question is as follows:
Route::group(array("domain"=>"{subdomain}.mydomain.com"),function(){
Route::group(array("before"=>"auth"),function(){
Route::get("logout",array("as"=>"logout",function(){
Session::flush();
Redirect::route("login");
}));
});
Route::group(array("before"=>"guest"),function($subdomain){
Route::get("login",array("as"=>"login",function($subdomain){
return View::make('login');
}));
});
});
The "logout" route redirects to http://dev.mydomain.com/http://%7Bsubdomain%7D.mydomain.com/login
While I had anticipated:
http://dev.mydomain.com/login
When I remove the outer route group (the one grabbing the subdomain as a parameter), everything works as expected.
Any help is greatly appreciated
I solved this by using Redirect::to('login') instead of using named routes.
It only happens when using Redirect::route('login').
It may be related to your domain name. If you use local domain, you may use invalid character inside it such as "_", "+", "," etc.
I've read countless articles about this issue in Laravel 4. Not one of the solutions worked for me. You cannot URL::to('login') and remove the subdomain when inside the subdomain. Laravel remembers it.
This is how you should "get outside" of the subdomain.
First you should name the root route and also define your main domain.
Route::group(['domain' => 'yourwebsite.com'], function()
{
Route::get('/', ['uses' => 'YourController#getIndex', 'as' => 'home']);
});
Second you can construct the route by yourself:
URL::route('home').'/login'
That would be enough to resolve the L4 subdomain routing problem.
If you want to automatically redirect to the the non-subdomain URL. You can do this at the end of your subdomain handling:
Route::group(['domain' => '{subdomain}'.'.yourwebsite.com'], function() {
// Here goes all your subdomain handling
// Then handle subdomain requests that where not found
Route::get('{slug}', function($subdomain, $slug) {
return Redirect:to(URL::route('home').'/'.$slug);
})->where('slug', '.*');
});

Laravel 4: Unable to generate a URL for the named route "login" as such route does not exist

I'm creating an authorization system in my Laravel 4 project. I am trying to use the auth "before" filter.
In my routes.php file, I have:
Route::get('viewer', array('before' => 'auth', function() {
return View::make('lead_viewer');
}));
Route::get('login', 'LoginController');
The before filter calls this line in the filters.php file:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('login');
});
I can manually navigate to my login route. But the auth system isn't letting this happen. I've run composer dump-autoload a couple of times, so that isn't the problem. What am I doing, since I can actually load the login page if I do it manually?
I figure it out. Laravel is looking for a named route: I had to do this:
Route::get('login', array('as' => 'login', function() {
return View::make('login');
}));
An interesting, not very intuitive approach in Laravel. But there must be a reason Taylor did this that I'm not seeing.
To do what you were trying to do in your initial approach you could have just done:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('/login');
});
and it would have worked just fine.
If you want to use named routes then you do what you posted in your answer to your own question. Essentially...more than one way to skin a cat.
Hope that helps
I know you've probably solved this by now but after stumbling across your post while trying to solve a similar problem, I wanted to share my thoughts...
Laravel is NOT looking for a named route for the guest method, it is expecting a path.
Your example works because because the named route and the path are the same i.e. "login". Try changing your URL to something other than 'login' and watch it fail.
If you want to use a named route you should use the route helper method as so...
if (Auth::guest()) return Redirect::guest( route('login') )
Hope that helps someone.

Resources