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');
}
Related
I try to display a view but I get a 404 not found error, but my code seems correct
this is the route created
Route::get('talk',[\App\Http\Controllers\TalkController::class,'index'])->name('talk.index');
the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class TalkController extends Controller
{
public function index(): view
{
return view('talk');
}
}
here is what I got as a result
enter image description here
need help please
I was Facing the Same Problem laravel Store cache and if you stop the serv and start it all over you will still see the 404 so its better to do :-
php artisan optimize
php artisan optimize creates a compiled file of commonly used classes in order to reduce the amount of files that must be loaded on each request. The file is saved to, or overwrites, bootstrap/cache/compiled. php , which needs to be writable by the web server (PHP process).
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 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');
After reading some tutorials on laravel 5.4 authentication (including the doc),I don't know how to work with it in my file.
I have been able to run the artisan command.. php artisan make:auth. Have seen the the controller, views etc that was created and even have accessed it by going to http://localhost/blogsite/public/register (don't worry about, its on my local disk) but how do I integrate it with with the pages that needs authentication? That I don't know..
Who can put me through how to integrate it with other pages
Many way you can use for this solution.
First Way:
If you load views file from controller just use the following line to your controller.
Suppose my controller name is DashBoardController
public function __construct()
{
$this->middleware('auth');
}
So all of the view you return from DashboardController it will make you auth for you. That means if you return any of view from this controller you must need to log in.
So you need to put this constructor function to all of your Controller from where you return view and need to authenticate the user.
To avoid this constructor funtion to all controller you can use the following
Way using route:
Route::group(['middleware' => 'auth'], function () {
Route::Your_Request_Method('your_url1', 'YourController1');
Route::Your_Request_Method('your_url2', 'YourController2');
});
You can get more way at laravel authentication documentation
Hope you will understand.
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