I am unable to access Laravel view - php

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');

Related

Method App\Http\Controllers\LanguagesController::index does not exist

LARAVEL
when I make a controller insid folder called "Admin" by command
"php artisan make:controller Admin\ControllerLanguages"
and Route the page This error appears:
Method App\Http\Controllers\LanguagesController::index does not exist.
Bad Method Call Did you mean
App\Http\Controllers\LanguagesController::validate() ?
but when i make the controller normally in the default folder by command:
"php artisan make:controller LanguagesController"
the route runs and the page appears, i want the page appears when i create it in "Admin" Folder, I tried many ways, but to no avail.
You should declare the namespace for the route group
Route::prefix('languages')
->namespace('App\Http\Controllers\Admin')
->group(function() {
Route::get('/', 'LanguagesController#index')->name('admin.languages');
//All other Routes for languages defined here
//LanguagesController is at app/Http/Controllers/Admin folder
});
Or you can import the namespace via use statement like, at the top
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\LanguagesController;
Route::prefix('languages')
->group(function(){
Route::get('/', [LanguagesController::class, 'index'])->name('admin.languages');
//Other languages routes here
});

LARAVEL Returning Blank View With No Error

When I click the link leading to /[page]/ffacts, where [page] is one of zagor, dylan_dog or superman, I get a blank page.
The view is in resource/views/[page]/ffacts.blade.php
These are the routes:
Route::get('/', 'PagesController#home');
Route::resource('zagor', 'ZagorController');
Route::resource('dylan_dog', 'DylanDogController');
Route::resource('superman', 'SupermanController');
Route::get('/zagor/ffacts', 'ZagorController#ffacts');
Route::get('/dylan_dog/ffacts', 'DylanDogController#ffacts');
Route::get('/superman/ffacts', 'SupermanController#ffacts');
Code in Controllers:
public function ffacts()
{
return view('Superman.ffacts', compact('superman'));
}
Change it like this:
Route::get('/', 'PagesController#home');
Route::get('/zagor/ffacts', 'ZagorController#ffacts');
Route::resource('zagor', 'ZagorController');
Route::get('/dylan_dog/ffacts', 'DylanDogController#ffacts');
Route::resource('dylan_dog', 'DylanDogController');
Route::get('/superman/ffacts', 'SupermanController#ffacts');
Route::resource('superman', 'SupermanController');
Problem is, that /zagor/ffacts get matched by Route::resource('zagor', 'ZagorController'); because resource generate all routes for all CRUD operations, so also something like this /zagor/{id}
And this is matched before your custom. You can also check this by running artisan command: php artisan route:list
From what I can see, if your directory structure is really
resource/views/page/ffacts.blade.php
then you will need to be using the following return command for the view
public function ffacts()
{
return view('page.ffacts', compact('superman'));
}
To use return view('Superman.ffacts') you would need a directory structure of resources/views/Superman/ffacts.blade.php

laravel making its own route to blade file

working a project with Laravel 5.6.
the problem is, when im going to an url like:
/login
or any other route and specify where it should go, it making its own route and going to another place. no metter if i even clear the code of that blade file.
i have not several routes or blade file that are the same. i have cleared my browser cache, laravel cach, config cache, and the command:
php artisan route:cache
did not worked to clear route cache.
my code example: web.php code
Route::get("/login", "LoginController#login");
Example: LoginController.php code
public function login()
{
return view('/login'); // not going to this path
}
to conclude, it does not read my code :(
need your ideas!
public function login()
{
return view('login');
}
view accepts the view name not the path of the route, If you want go to any route use redirect("/route_name") . But in your case if you redirect to login route again it will throw exception because the login route again calls this function. so you need to pass the view name. for example:
if your login view is in
resources
- views
-login.blade.php
then use above code. or if the login page is in any other folder inside view
it will be like return view("foldername.login")

Showing wrong view in laravel 5.4

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.

Change laravel welcome file name

I changed the files path after i installed laravel framework like this:
from:
resources/views/welcome.bandle.php
to
resources/views/admin/index.php
and the routes file to:
Route::get('/admin', function () {
return view('admin/index');
});
the url is working
but all the larvael render not working
like this:
what i need to do?
tnx a lot.
You need to add the .blade.php extension to the files you want to parse using "Blade Engine", that will remove all the tags you have within curly braces.
Next, you need to write your route like this:
Route::get('/admin', function(){
return view('admin.index');
})->name('admin.index')->middleware('auth');
It is a good convention naming the routes for easy access across the application, that way you can simply reference it in the blade views like this:
Admin page
That way you will have the dynamic route no matter from where in the file structure you call it.
Or you can also use your Controller to display such view. By this you'll write your routes more cleaner. Let's say we have an AdminController that handles all admin processes and functions. Put your dashboard.blade.php inside views/admin directory.
The route:
Route::get('/admin', 'AdminController#index');
The controller:
class AdminController extends Controller
{
public function index()
{
return view('admin.dashboard'); // in views->admin->dashboard.blade.php
//add some data here
}
}
Just keep 'blade' in view file name if you don't plan to use controller, e.g.:
resources/views/admin/index.blade.php

Categories