NotFoundHttpException in Handler.php line 103: - laravel-5

I'm using L5.2.
I'm busy trying to create a shopping cart and I've ran into a problem that I can't seem to figure out how it's coming about.
What is suppose to happen is after I've added products to the shopping cart, I click on the shopping cart link and I'm suppose to get taken to another page that says "getCart" echoed on the page.
What is happening is that once I click on the shopping cart link I'm getting the error below and I don't see how I could be getting that error if I'm only echoing out "getCart".
The error I'm getting is
NotFoundHttpException in Handler.php line 103: No query results for model [App\Modules\Menus\Models\Menu].
My routes.php
Route::resource('/', 'OpenController');
Route::get('/{id}', 'OpenController#content');
Route::get('/add-to-cart/{id}', [
'uses' => 'OpenController#getAddToCart',
'as' => 'product.addToCart'
]);
Route::get('/shopping-cart', [
'uses' => 'OpenController#getCart',
'as' => 'product.shoppingCart'
]);
My OpenController.php
namespace App\Modules\Open\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Modules\Menus\Models\Menu;
use App\Modules\Portfolio\Models\Portfolio;
use App\Modules\Products\Models\Product;
use App\Modules\Open\Models\Cart;
use Session;
class OpenController extends Controller
{
public function getAddToCart(Request $request, $id){
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->back();
}
public function getCart(){
echo "getCart";
}
}
products.blade.php
<li>
<a href="{{ route('product.shoppingCart') }}">
<i class="fa fa-shopping-cart" aria-hidden="true"></i> Shopping Cart
<span class="badge">{{ Session::has('cart') ? Session::get('cart')->totalQty : '' }}</span>
</a>
</li>
UPDATE:
I've kind of managed to fix it, but I'm hoping someone can still help me out.
I changed my route from
Route::get('/shopping-cart', [
'uses' => 'OpenController#getCart',
'as' => 'product.shoppingCart'
]);
to
Route::get('/products/shopping-cart', [
'uses' => 'OpenController#getCart',
'as' => 'product.shoppingCart'
]);
Can someone explain why it wouldn't work with just the /shopping-cart

It wouldn't work because "/products/shopping-cart" is a different route that "/shopping-cart".
And since you call route "product.shoppingCart" you also have to have that route declared.
Hope that explained it.

Related

Keep getting "MethodNotAllowedHttpException" when trying to display a page redirected from an external URL

I'm trying to reach to my local page from an external URL, which is from a payment gateway, based on the return URL parameter. However, when I link it back to my page, I keep getting the "MethodNotAllowedHttpException" error. Below are my codes;
Order Controller:
public function kiplestatus(Request $request, $id) {
dd($request);
$showFeedbackForm = true;
$order = \App\Order::where('id', $id)->get();
}
Route:
Route::get('/order/kiplestatus/{id}', [
'uses' => '\App\Http\Controllers\OrderController#kiplestatus',
'as' => 'order.kiplestatus',
'middleware' => ['auth','verify.mobile.no','agent'],
]);
Am I doing anything wrong here?

auth logout doesn't work laravel

I made the files for authentication using the command
php artisan make:auth
I've read on the internet that register, login, as well as logout should work properly, but localhost:8080/logout doesn't work, and I don't know why.
I also read something about modifying AuthController in app, but I do not have that file.
I tried to do it by hand, which means I created a middleware LogoutRedirect:
public function handle($request, Closure $next)
{
return redirect(pages.logout);
}
In the routes I added
use App\Http\Middleware\LogoutRedirect;
Route::get('logout', function()
{
return view('pages.logout');
})->middleware(LogoutRedirect::class);
And logout.blade.php looks like
{{ Auth::logout() }}
I get the error (when trying to access localhost:8080/logout)
Use of undefined constant pages - assumed 'pages'
What could I do about it?
EDIT
I tried another approach (but with no better results):
renamed the route which redirects to '/' to 'home'
made a LogoutController in app/http/Controllers/Auth
namespace App\Http\Controllers;
use [...]
class LogoutController extends Controller
{
public function logout() {
Auth::logout();
return Redirect::route('home');
}
}
made the route
Route::post('logout', array(
'as' => 'account-sign-out',
'uses' => 'Auth\LogoutController#logout'
));
The error I get is
MethodNotAllowedHttpException in RouteCollection.php line 233:
That's the same error I get when I try to use the default logout defined in auth
You are trying to access the logout page with GET. But this doesn't work because your logout route is a post route.
Change
Route::post('logout', array(
'as' => 'account-sign-out',
'uses' => 'Auth\LogoutController#logout'
));
by
Route::get('logout', [
'as' => 'account-sign-out',
'uses' => 'Auth\LogoutController#logout'
]);
When you go to the /logout route with the method GET(The default when you go to a page) it should work.

laravel 5.1 error controller Missing argument 1

i have a foreach with my list of products in my index.blade.php, it work well, now i'm tryng to filter, i done my menu with my categories and genders.
I would like show products with category = "t-shirt" and gender= "woman" but i have this error:
ErrorException in StoreController.php line 36: Missing argument 1 for
dixard\Http\Controllers\StoreController::products()
i'm using this link:
<a href="{{url('shop', ['category'=> 't-shirt', 'gender' => 'woman'])}}" title="">
<span>Woman</span>
</a>
my route:
Route::get('shop', 'StoreController#index');
Route::get('shop/{category}/{gender}','StoreController#products');
My controller
public function products($category, $gender)
{
$gender_id= Gender::where('gender', $gender )->first();
$category_id= Category::where('name', $category)->first();
$filter = ['gender_id' => $gender_id->id, 'category_id' => $category_id->id];
$products = Product::where($filter)->orderBy('id', 'asc')->get();
$categories = Category::all();
return view('store.index', compact('products','categories'));
}
You can use named routes. There's nothing special with this.
Route::get('shop/{category}/{gender}', [
'uses' => StoreController#products',
'as' => 'shopRoute'
]);
And your URL:
route('shopRoute', ['category'=> 't-shirt', 'gender' => 'woman'])
Use the route function (https://laravel.com/docs/5.1/routing)
<a href="{{route('shop', ['category'=> 't-shirt', 'gender' => 'woman'])}}" title="">
<span>Woman</span>
</a>
i fixed like this:
Route::get('shop','ShopController#index');
Route::get('shop/{categoryA}','ShopController#category');
Route::get('shop/{categoryB}/{genderB}','ShopController#categoryGender');
Route::get('shop/{categoryC}/{genderC}/{slugC}','ShopController#product');
I dont know why but i changed that of variables that i pass to my route and it work!

Build menu through Route::group in Laravel 5

I have 2 Route::group and each one has a couple of routes. For example:
Route::group(['prefix'=>'/guest', 'middleware'=>'guest'], function()
{
Route::get('login', ['as' => 'Login to the site', 'uses' => 'WelcomeController#login']);
Route::get('register', ['as' => 'Register', 'uses' => 'WelcomeController#register']);
Route::get('restore', ['as' => 'Restore the password', 'uses' => 'WelcomeController#restore']);
});
Route::group(['prefix'=>'/admin', 'middleware'=>'auth', function()
{
Route::get('/dashboard', ['as' => 'Home Page', 'uses' => 'AdminController#users']);
Route::get('/users', ['as' => 'Users', 'uses' => 'AdminController#users']);
});
So, I would like automatically to build a menu using the current prefix of Route::group. If user is authenticated Laravel should display the menu list like this:
<li>Home Page</li>
<li>Users</li>
but if user is just guest, in this case my menu should be like this:
<li>Login to the site</li>
<li>Register</li>
If you look through the second guest menu, you will see that menu for Restoring password was missed. Yes, I would like sometimes do not display some menus.
Basically, I have a 2 questions:
Find the routes which belong to the current Route group and build a
menu.
Don't display any routes in menu, adding some option to them.
Route groups are transient and their only use is to allow the router to populate in bulk the specific attributes (prefixes, namespaces, etc) of the routes that are in them.
When you register a group, the attributes you pass to it are added to the routes that are defined inside the group, then the group is deleted. So the group exists within the router only while the Route::group method is being executed.
All that means that you can't get any group information, in your route closure or controller method in order to get the routes inside.
Since you say that you have 3 types of users, then you should probably have a way to get the user type of the authenticated user, with something like Auth::user()->type (which would return one of these values admin, instructor, student). Also, you should be able to use Auth::guest() to determine if a user is not logged in.
So you can just do the following to handle menu generation (the code below assumes that the $user variable contains the model from Auth::user()):
<ul>
#if (Auth::guest())
<li>Login to the site</li>
<li>Register</li>
#else
#if ($user->type == 'admin')
<li>Home Page</li>
<li>Users</li>
...
#elseif ($user->type == 'instructor')
<li>Home Page</li>
<li>Students</li>
...
#elseif ($user->type == 'student')
<li>Home Page</li>
<li>Lessons</li>
...
#endif
#endif
</ul>
I added "#" in alias for some routes which I want to hide.
Route::group(['prefix'=>'/guest', 'middleware'=>'guest'], function()
{
Route::get('login', ['as' => 'Login to the site', 'uses' => 'WelcomeController#login']);
Route::get('register', ['as' => 'Register', 'uses' => 'WelcomeController#register']);
Route::get('restore', ['as' => 'Restore the password#hide', 'uses' => 'WelcomeController#restore']);
});
Then in view I added this code:
foreach( Route::getRoutes() as $route){
$data = explode('#', $route->getName());
if(Route::getCurrentRoute()->getPrefix() == $route->getPrefix() && #$data[1] == "" ){
echo '<li>'.$data[0].'</li>';
}
}
Now, I successfully displayed all routers of current route::group and hide some routes adding # to the alias. In example it is Restore the password#hide

Do i have to define every controller method in routes.php?

This is my current routes.php file:
<?php
Route::get('/', 'AdminController#index');
Route::get('/posts','PostsController#index');
Route::get('/posts/create','PostsController#create');
Route::get('/tags','TagsController#index');
Route::get('/health','HealthController#index');
Route::get('/health/create','HealthController#create');
Route::get('/health/categories','HealthController#categories');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController'
]);
If i dont do that for exmaple, i canbt use helpers in blade views , like action. It throws a null exception.
So the question is, do i have to define here all the controller actions? Or else i will not be able to use them directly? For example in a redirect to action link.
Adding this to blade:
<a href="{{ action('PostsController#index') }}">
throws an exception UNLESS i specifically add the route with Route::get NOT working if i add an entry to Route::controllers.
Tried also
<a href="{{ action('\App\Http\Controllers\PostsController#getIndex') }}">
<a href="{{ action('\App\Http\Controllers\PostsController#index') }}">
The problem here are your controller action names. If you use implicit controller routes (Route::controllers) your method names have to start with an HTTP verb.
Instead of index() you need getIndex().
You can easily check what routes Laravel actually registers using the php artisan route:list command.
Generating an URL would then look like this:
<a href="{{ action('PostsController#getIndex') }}">
Note Controller routing only works if you add PostsController to the Route::controllers. (I assumed you did so but just to be sure)
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
'posts' => `PostsController`
]);

Resources