Any help why this is not working ,I am using Laravel 5.5.23 version ,this is are my routes :
<?php
Route::get('/', function () {
return view('welcome');
});
Route::resource('threads','ThreadController');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('threads','ThreadController#index');
Route::get('threads/{channel}','ThreadController#index');
Route::get('threads/create','ThreadController#create');
Route::get('threads/{channel}/{thread}','ThreadController#show');
Route::post('threads','ThreadController#store');
Route::post('/threads/{channel}/{thread}/replies','ReplyController#store');
Route::get('/logout' , 'Auth\LoginController#logout');
This is the ThreadController ,just the relevant methods actually :
public function __construct()
{
$this->middleware('auth')->except(['index','show']);
}
public function index($channel = null)
{
if($channel){
//do something
}
else{
$threads=Thread::latest()->get();
}
return view('threads.index',compact('threads'));
}
The problem is when I try to access /threads/someChannel it returns not found ,so this is the problematic route : Route::get('threads/{channel}','ThreadController#index'); ,all other routes are working ,any idea why this one is not working ?
The Route::resource('threads','ThreadController') call is defining 7 routes with the prefix threads. Some of the routes you are defining yourself are being masked by this. Check php artisan route:list to see what routes the Route::resource call registers for you first. First come first serve when it comes to matching routes.
/threads/someChannel is going to match the route defined by the resource call:
GET /threads/{thread} ThreadController#show
Since you don't have the definition for show which is relevant to have, I would assume you have Implicit Model Binding happening. It is trying to bind that model to that parameter and it can't find it in the database by that ID and is causing a 404 because of it.
Related
i replace my web.php whit this code, same as my code in laravel 5.2, now im using laravel 5.5, i dont have any errors in 5.2 version.
Route::get('/home', function () {
return view('home');
});
Route::get('/register', 'registerController#index');
Route::post('/register', 'registerController#register');
Route::get('/signin', 'signinController#index');
Route::post('/login', 'signinController#login');
Route::get('/logout', ['uses'=>'signinController#logout'])->middleware('auth');
Route::get('/profile', ['uses'=>'profileController#index'])->middleware('auth');
Route::get('/updateprofile', ['uses'=>'profileController#updateprofile'])->middleware('auth');
Route::post('/updateprofile', ['uses'=>'profileController#updateprofilesave'])->middleware('auth');
Route::post('/updateprofiles', ['uses'=>'profileController#updatechannelart'])->middleware('auth');
Route::get('/changepassword', ['uses'=>'profileController#indexpassword'])->middleware('auth');
Route::post('/changepassword', ['uses'=>'profileController#changepassword'])->middleware('auth');
Route::get('/article', 'articleController#index');
Route::get('/searchuser', ['uses'=>'searchController#index']); //Untuk searching user
Route::get('/searchuserpage', ['uses'=>'searchController#searchuser']); //searching user jquery
Route::get('/photos', ['uses'=>'documentationController#indexphoto'])->middleware('auth');
then i try to access url /profile which means need authenticate first, and it show me an error InvalidArgumentException Route [login] not defined. how to solve this problem. thankyou
this is my code for Authenticate.php
public function handle($request, Closure $next)
{
if(Auth::Check()){
return $next($request);
}
else{
return redirect('/signin');
}
}
The issue comes from the fact that somewhere in your code upon instantiation you're referring to a named route called 'login' but it's not defined in your web.php file.
An example of hitting this issue is you may have a redirect pointing to this route somewhere tucked away in one of your controllers, for example:
return redirect()->route('login');
To fix this issue, apply the name to the applicable route.
Route::post('/login', 'signinController#login')->name('login');
when you call a route in your project you must define route name .
such as :
<form action:"{{route('login')}}" method="post">
and in route :
Route::post('/signin', 'signinController#index')->name('login')
This is issue with the named routes. Please make sure which all places the named routes is being used.
Route::get('/signin', 'signinController#index')->name('login')
Here, you can see I named this route login and I can call this route anywhere using route('login') helper method.
I have a single domain/subdomain project. In order to see the event by slug, I made this route:
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
});
After my page didn't load correctly, I did a dump in the controller:
public function getView($slug)
{
return $slug;
}
To get to the route I am using this URL: https://example.com/events/slug-example.
The problem is that the route is being hit as I see the response when I change it, but I am not getting the slug, instead I am getting Region object back.
If I do this:
public function getView($region, $slug)
{
return $slug;
}
Then I get the slug back. But I have no idea how is this possible, and how could I do it (I came as another dev on the existing project).
I tried commenting out all the middleware and it is still the same. How can I even make something fill the method if I didn't explicitly say it?
EDIT
I noticed there is binding going on in routes file:
Route::bind('region', function ($value) {
...
});
Now if I dd($value) I get the variable back. How is this value filled? From where could it be forwarded?
Looking quickly it should work, but maybe you was verifying other url.
Make sure you put:
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
routes at the end of routes you showed.
EDIT
If you think that's not the case and you don't have your routes cached you should run:
php artisan route:list
to verify your routes.
EDIT2
After explaining by OPs in comment, domain used for accessing site is:
{region}.example.com
So having $region in controller as 1st parameter is correct behaviour because of route model binding and other route parameters will be 2nd, 3rd and so on.
Instead of
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
});
try
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
});
I have this code in api.php routes file for laravel 5.4
Route::get('/lectures/{id}', function ()
{
dd("lecture route");
});
Route::get('/lectures/send-request', function ()
{
dd("send-request route");
});
the problem is that when I visit localhost:8000/api/lectures/send-request the output is 'lecture route' which is for this url localhost:8000/api/lectures/{id}
but when I change the orders of routes it will work properly
Route::get('/lectures/send-request', function ()
{
dd("send-request route");
});
Route::get('/lectures/{id}', function ()
{
dd("lecture route");
});
now when I visit localhost:8000/api/lectures/send-request the output is 'send-request route'
so what is wrong ? why this is happening ?
The routes are working exactly as intended. The routes take precedence the same way they're defined.
GET /lectures/{id}
GET /lectures/send-request
You've defined two routes where the second route is basically one of options of the first route. When you access /lectures/send-request it satisfies the condition for the route /lectures/{id} with id set to send-request.
You can switch the order of routes based on your preference, or you could change one of the route.
This is because Laravel matches routes top-down. This means that the first route to match is the one being used. /lectures/send-request matches '/lectures/{id}' and sets $id to 'send-request'.
Let's say I am defining the following route in Laravel 5.3:
Route::resource('bands', 'BandController');
The default route example coming when you start a new Laravel project has the following:
Route::get('/', function () {
return view('welcome');
});
How do I make bands route to be the default one when no controller is called instead of welcome? Meaning /?
I did read docs here but didn't found anything. Any?
Place that block inside laravel/app/routes.php instead of a Controller (4.x)
Place that block inside laravel/app/Http/routes.php instead of a Controller (5.1)
Place that block inside laravel/app/routes/web.php instead of a Controller (5.3)
Route::get('/', function()
{
return view('welcome');
});
You can redirect default to anywhere you want, i.e.:
Route::get('/', function()
{
return Redirect::to( '/bands');
// OR: return Redirect::intended('/bands'); // if using authentication
});
As #Patrick mentioned, you can simply redirect to the /bands route. However, I have to warn you that the redirect will actually change the URL in the navigation pane of the web browser. I would have suggested that you just ask the home route to use the index method of your BandController as follows:
Route::get('/', ['uses'=>'BandController#index']);
So, to keep this short, how does laravel know what to do in the default controller when it sees the method 'showWelcome' ?
in routes there is
Route::get('/', function() { return View::make('hello'); });
which is fine, but how does it now to act on showWelcome.. where is that set in configs? could it be named something else?
If you're going by the default install of Laravel 4 (at the current time), the showWelcome method isn't actually fired on the HomeController controller for this route. However, the method would return the same thing as the route.
You could fire this method by changing to route to the following:
Route::get('/', 'HomeController#showWelcome');