Showing wrong view in laravel 5.4 - php

I am using laravel 5.4 and trying to get index page, i am using following routes
Route::get('/',
['as' => 'home_page',
'uses' => 'Controller#index']);
and index function in controller looks like this:
public function index()
{
return view('index');
}
But when I visit mydomain.com, I get a different view than index.blade.php.
and it is fine when I use mydomain.com/? or on my local server.
I have searched everywhere in my code and in a google, but didn't found anything, any help?
ie: let me know if any further information required.

First make sure you are calling the right controller, and this dont have a specific middleware blocking the acess to your index method and index.blade.php is inside view folder.
If all of this is fine try this code on your rotes file:
Route::get('', function () {
return view('index');
})

Try this.
First use the make:controller Artisan command to create a controller file. let's say it is homeController.
php artisan make:controller homeController
Then in the homeController file write your code to get the view.
<?php
namespace App\Http\Controllers;
class homeController extends Controller
{
public function index()
{
return view('index');
}
}
Then define a route to this controller.
Route::get('/', 'homeController#index');
For more information please refer https://laravel.com/docs/5.5/controllers

There was a cached view saved on my server, I used
php artisan cache:clear and it got fixed. Thank you everyone for the support.

Related

Laravel 8 Route not defined even route is defined

This error is comeup while i working on laravel 8
Route [product.details] not defined. (View: C:\xampp\htdocs\ecommerce\resources\views\livewire\shop-component.blade.php)
here is the route i made Route Page web.php
Route::get('/product/{slug}',DetailsComponent::class)->name('product.details');
this is line of view where i want that route View Page shop-component.balde.php
<a href="{{route('product.details',['slug'=>$product->slug]) }}" title="{{$product->name}}">
and this one is the Details Component code
namespace App\Http\Livewire;
use Livewire\Component;
class DetailsComponent extends Component
{
public $slug;
Public function mount($slug){
$this->slug = $slug;
}
public function render()
{
$product = Product::where('slug', $this->slug)->first();
return view('livewire.details-component',['product'=>$product])->layout('layouts.base');
}
}
I think this php code doesnot get route of product.details at the same time i create middleware file and define a different route in it this route is working in this page but not Product.details.
Kindly help me i am stuck from 1 week here
You can use
php artisan route:list
to show a list of registered routes - make sure it shows up there. If it doesn't, use
php artisan route:clear
to clear Laravel's route cache
It does not work because of your route. It's not write in the correct way. You have first to make your DetailsComponent::class is bracket then you have to give the name of the method you want to use in this class.
Route::get('/product/{slug}', [DetailsComponent::class, 'method name'])->name('product.details');

Laravel: Not picking up __invoke method?

Trying to use invokable controllers, but it seems to fail to find the __invoke method?
Invalid route action: [App\Http\Controllers\App\Http\Controllers\MainController].
It seems to be returning true on:
if (! method_exists($action, '__invoke')) {
throw new UnexpectedValueException("Invalid route action: [{$action}].");
}
Routes:
<?php
Route::get('/', \App\Http\Controllers\MainController::class);
MainController:
<?php
namespace App\Http\Controllers;
class MainController extends Controller
{
public function __invoke()
{
dd('main');
}
}
Laravel by default assumes that your controllers will be located at App\Http\Controllers\. So when you're adding the full path to your controller, Laravel will check it there, at App\Http\Controllers\App\Http\Controllers\MainController.
To solve it simply remove the namespace when you're registering the route, and register it like this:
Route::get('/', MainController::class);
Alternatively, you can stop this behavior by removing ->namespace($this->namespace) from mapWebRoutes() method on RouteServiceProvider class, which is located at App\Providers folder. Then you can register your routes like this:
Route::get('/', \App\Http\Controllers\MainController::class);
Alternatively, you can use:
use App\Http\Controllers\MainController;
Route::get('/', [MainController::class, '__invoke']);
In this case, the namespace provided in RouteServiceProvider won't be taken into account.
The advantage of this is that now your IDE will be able to reference the class usage and you can navigate by clicking on it.
The best answer that works for everyone is laravel documentation.
just use this at the top of your route(web.php) if (websiteController is the name of your controller)
use App\Http\Controllers\WebsiteController;
and define your route like this for your index page
Route::get('/', [WebsiteController::class, 'index']);
take note of the
[ ]
You have to create the crontoller with argument "--invokable"
php artisan make:controller YourController --invokable
Always Declare/Use in Top
use App\Http\Controllers\backend\DashboardController;
Then Use this way
Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard');
If have Auth and Use Group
Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
'verified'
])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard');
})
Change your route to:
Route::get('/', "MainController");
In my case I forget to set #action, so change your code from:
Route::get('admin/orders', 'Admin\OrderController')->name('admin.orders');
to:
Route::get('admin/orders', 'Admin\OrderController#index')->name('admin.orders');
you have mentioned get link, but you have not declared which method it should call.
Route::get('/', \App\Http\Controllers\MainController::class);// if you are importing lass like this you have to use resource instead of get.
you can solve this issue by two ways,
first way,
Route::get('/', '\App\Http\Controllers\MainController#index'); // you have to mention your method which you have mentioned in controller
another way is,
Route::resource('/', \App\Http\Controllers\MainController::class);
In, 2nd method laravel will automatically find which request and where should redirect.2nd option is prefered if you are using multiple method for the same route.
Route::resource('/', \App\Http\Controllers\MainController::class);
Use method 'resource'

Laravel 5.7 login with Laratrust, Routing fails with wrong route to "/home"

I wanted to give laratrust a shot, but I ran into some trouble with the routing.
Let me explain in brief, what I did so far.
I've created a new laravel app as described in the laravel docs. Then I used the make:auth generator to generate everything needed for standard login/registration. That works great. Now I am on a point, where laratrust kicks in. After installation I did everything as described in the laratrust docs. Additionally I went with DevMarketer's "Build an advanced blog" series on youtube, to get laratrust to work.
The problem is now, that a login (i.e. as admin) leads me to https://myapp.local/public/home instead to https://myapp.local/public/admin/dashboard as defined so in the controller. If I edit the url after login to reach the dashboard, it works well and the dash appears. Why not after my login attempt?! Any ideas are welcome.
The urls above are just examples. So don't be confused while have a look into my "AdminController":
<?php
namespace App\Http\Controllers;
use App\User;
use app\Role;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function index()
{
return redirect()->route('administration.dashboard');
}
public function dashboard()
{
return view('backend.dashboard');
}
My routes:
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
/**
* Admin routes
*/
Route::prefix('administration')->middleware('role:superadministrator|administrator')->group(function () {
Route::get('/', 'AdminController#index');
Route::get('/dashboard', [
'uses' => 'AdminController#dashboard',
'as' => 'administration.dashboard',
]);
});
So, maybe its too late for me and I won't see my mistake. Can anybody help me out? Thanks guys. Good night.
Sorry guys,
I found the answer myself. I just saw, that I forgot to customize the LoginController.

I am unable to access Laravel view

I have just created one of about view in view/about.blade.php, and I am accessing this from localhost/myproject/public/about, but it's not working.
However, localhost/myprojects/public/ is working fine; about view has been created on same parameters as welcome by default in Laravel.
Firstly the information is not sufficient to say anything.Please provide your route.Also its important how you are running your project ,is it via Xampp(or Lampp whatever is there) or "php artisan serve"
but looking from your working directory "localhost/myprojects/public" I guess its not by the command . Try localhost/myprojects/public/about.blade.php or run it by php artisan serve and try route localhost:8000/about
Have you added particular routing to web.php file?
Route::get('about', function () {
return view('about');
});
https://laravel.com/docs/5.7/routing
Which error are you getting?
404 - Not found
Route::get('/about', function () {
return view('about');
});
Check routes
php artisan route:list
Laravel is a MVC Framework, Which means You Have a Controller which procede some logic when some request come in and interact with the model if need, after that the controller return some view.
And because you whan to acccess your view file, you must past through controller, and that controller will render the view. Because the views folder is not in the \public dicretory as subdirectory you can't access to It with url like localhost/myproject/public/about even if you get access to it, you will not get HTML, you'll get some plain text with Blade tags. It's a must to return view in you controller by rendering it, somewhere in the background Laravel procede all Blade Tag and return HTML that correspond to that tags.
What I can suggest you Is to create some route in your route file like this
Route::get('/about', function(Request $request){
// Automatically Laravel will look this file in the view directory
return view('about');
});
Or you can go with the controller like procedure by creating some controller, go in your terminal and execute
php artisan make:controller AboutController
this will generate a file name AboutController.php in app\Http\Controllers diretory within witch you will found
namespace App\Http\Controllers;
class HomeController extends Controller
{
}
after that add
public function index()
{
return View::make('about');
}
Don't forget to include use the Illuminale\Supports\Facades\View on top of your file
And one more important thing which left is to configure the Route, for that go in routes directory in the web.php file add
Route::get('/about', 'AboutController#index')->name('about');

404 in Controller Action Method : Laravel 5.2.37

I have following Controller Action Method.
namespace App\Http\Controllers\API\SportsType;
class SportsTypeApiController extends \App\Http\Controllers\Controller
{
public function apiSportsTypes() {
return 1;
}
}
Here is the route
Route::group(['prefix' => 'api/v1'], function () {
Route::get('/apiSportsTypes', 'API\SportsType\SportsTypeApiController#apiSportsTypes');
});
It gives 404 error. Am I missing something? Please let me know if you need more details.
It was happening because the routes were using cache. Due to that new routes were not even appearing in command prompt even if I was typing below command.
php artisan route:list
Then I had to remove the route cache by using the below command
php artisan route:clear
and now everything is working fine.

Categories