I am very new to Laravel. I created new Controller as
class ContactController extends Controller {
public function index(){
die("X");
}
}
And in the routes.php I wrote
Route::get('contact', 'ContactController#index');
I think hitting the following url the "X" must be printed. But it says "Not Found" error.
http://localhost/lapp/public/contact/index
What thing I am missing? Please guide me.
If the apache module_rewrite is not activated. Activate it. It will surely work.
Your guess is right. But the problem is with you URL.
it should be http://localhost/lapp/public/contact
Alternatively you can use php artisan serve command to start a server which would start the server on something like http://localhost:8080 Then you can access the URL like: http://localhost:8080/contact
Related
I was tried to find the problem, but still not found what is wrong on my code.
Can anybody help what is wrong on my code.
route (web.php)
Route::get('/pegawai','PegawaiController#index');
PegawaiController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PegawaiController extends Controller
{
public function index()
{
//mengambil data dari table pegawai
$pegawai = DB::table('pegawai')->get();
// mengirim data pegawai ke view index
return view('index',['pegawai' =>$pegawai]);
}
}
My database is using mysql and for another program is can run just for this is 404 not Found.
I'm using laravel 8.6
And I tried to create new project and is no problem, but on existing project is always 404 Not Found
Any idea why this happen??
change route to :
Route::get('/pegawai', [PegawaiController::class, 'index']);
and check htaccess file exist in public folder
run php artisan cache:clear
if you are using the latest version of laravel, then you should use the path of laravel in route like ---
Route::get('/pegawai',[\App\Http\Controllers\PegawaiController,'index']);
if show some error please remove \ in the controller path.
Hope it works for you.
I was solved this case,
just code on command prompt php artisan route:cache and then my code is work
You should contain the namespace in Route::get facade function second parameter.
AS-IS
Route::get('/pegawai','PegawaiController#index');
TO-BE
Route::get('/pegawai','App\Http\Controllers\PegawaiController#index');
or
use App\Http\Controllers\PegawaiController;
Route::get('/pegawai', PegawaiController::class);
I just installed Laravel 8 and created new controller and route. when i try to use new route that i created which is working fine but route('/') is not working. giving me error
The GET method is not supported for this route. Supported methods: HEAD.
route/web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('login', [LoginController::class,'loginShow'])->name('login');
LoginController
class LoginController extends Controller
{
function loginShow(){
return view('Login.login');
}
}
Route List
Problem Detail :
here i have 2 routes 1) mydomin or mydomin/ 2) myDomian/login
here myDomian/login is working as i want but when i try to use mydomin then i am getting
many time i face thing problem but sometime i fix anyway. But i want to know its real reason why it happen? so next time i will take case self. here mydomin is GET method route and i delcleared in web.php as GET. so why it is telling it is HEAD not get? Also note that before create a new route it was working in same way. so why now not? please tell me reason of this.
i got my solution using php artisan serve . so after that it is start working. but i a could not understand why it is not working without php artisan serve .... if any one has any reason please tell us.
If we just create a 404.blade.php page in resources/views/error it will work fine, but Auth() won't work on 404 page, to solve that if we follow the solution available on stackoverflow the Laravel auth errors will stop working. I use the following solution to do the work.
Create custom view
resources/views/errors/404.blade.php
in route.php
Route::any('{catchall}', 'PageController#notfound')->where('catchall', '.*');
create PageController and add this function
public function notfound()
{
return view('errors.404');
}
For Laravel 5.6 and later, you can use fallback in your routes\web.php:
Route::fallback('MyController#show404');
It works as an "catch all"-route.
See docs here.
Write below code in your Exceptions/Handler.php
if($this->isHttpException($exception)){
if(view()->exists('errors.'.$exception->getStatusCode())){
$code = array('status'=>$exception->getStatusCode());
return response()->view('errors.404',compact('code'));
}
}
Now create a new file in your view i.e errors/404;
Now in code array you can pass dynamic values
Not sure if this will help. Laravel has a PHP artisan command to publish error pages. Laravel Custom HTTP Error Pages
After you run this artisan command use Route:fallback() method as #KFoobar suggested. If you use a closure function, no need to use a controller. Make sure to add the below route at the ends of your routes file.
//Fallback/Catchall Route
Route::fallback(function () {
return view('errors.layout');
});
Shameless plug for my BLOG
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');
Just started learning laravel I tried to create a route for my view but when I load on the web browser it says Sorry, the page you are looking for could not be found. Can any one help me out in codeigniter it was so simple just create a controller and view and we can see on the web broswer I found laravel to be difficult then codigniter is that true?? can anyone define me how is the mvc structure for laravel5 as i found tutorials but they are of old laravel and files and structure are almost change so I got confused any suggestions please
routes.php
Route::get('main', 'Main#index');
Main.php
namespace App\Http\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class main extends Contoller {
public function _construct() {
$this->middleware('guest');
}
public function index() {
return "Hello World from controller";
}
}
if you are running laravel project locally, it can run through its own server. you dont need apache server form wamp or xampp,,but you will need their mysql database. So start only that if you require database.
Now go to command prompt, navigate to the directory where your project is stored eg cd c:/wamp/www/yourprojet and then type following command
php artisan serve
it will start on port 8000 by default. and you can now access your project at 'http://localhost:8000/'
and you can access your view at 'http://localhost:8000/main'
Also you can find laravel tutorials and other at laracast
Try to change the class name to Main (now is main, in lowecase)
I learnt Laravel by their awesome tutorial: https://laravel.com/docs/4.2/quick
make a view in resources->views, something like my-view.blade.php
Then return view('my-view');
That my-view.blade.php can have whatever HTML you want in it
Go to your resources/views folder create file with filename.blade.php.
Now in your routes.php:
Route::get('main', 'Main#index');
And in your controller add this function:
public function index() {
return view('filename');
}