I'm trying to follow laracasts tutorial on laravel fundamentals but after getting composer and laravel installed with no problems I can't get my routes file to work with the controller I've reinstalled laravel copied it exactly how laracasts has his but still nothing, anyone see anything wrong with these two files?
routes.php files
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', 'Controller#index');
Route::get('contact', 'Controller#contact');
controller.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
abstract class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;
public function ___construct()
{
$this->middleware('guest');
}
public function index()
{
return 'hello world!';
}
public function contact()
{
return 'Contact me!';
}
}
I have it hosted on localhost:8888 using phps server command if that is any help.
The reason might be that your controller class is abstract, hence it's not instantiable. Remove the abstract keyword.
if your route is get or post and actually you have implemented resource, so you get this type of error
Related
So I am trying to create some blade templates using Laravel 6. I have a number of blade templates set up and working correctly but for the some reason, I am faced with an error 404 anytime I try to access the settings pages by link or by URL.
My directory is set up such that
- projectName
-- app
--- Http
---- Controllers
----- Controller.php
----- HouseController.php
-- resources
--- views
---- pages
----- emergencySettings.blade.php
----- personalSettings.blade.php
----- roommatesSettings.blade.php
----- informationSettings.blade.php
----- socialsSettings.blade.php
----- many other blade.php files which all work correctly
---- welcome.blade.php
-- routes
--- web.php
I have set up the routes in the web.php file like so
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
use App\Http\Controllers\HouseController;
Route::get('/pages/emergencySettings', [HouseController::class, 'emergencySettings']);
Route::get('/pages/personalSettings', [HouseController::class, 'personalSettings']);
Route::get('/pages/roommatesSettings', [HouseController::class, 'roommatesSettings']);
Route::get('/pages/socialsSettings', [HouseController::class, 'socialsSettings']);
Route::get('/pages/informationSettings', [HouseController::class, 'informationSettings']);
The controller is set up as follows
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
The HouseController extends Controller and is set up as follows
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\House;
class HouseController extends Controller
{
public function emergencySettings(){
return view('pages.emergencySettings');
}
public function informationSettings(){
return view('pages.informationSettings');
}
public function personalSettings(){
return view('pages.personalSettings');
}
public function roommatesSettings(){
return view('pages.roommatesSettings');
}
public function socialsSettings(){
return view('pages.socialsSettings');
}
}
I would like to reiterate that this already includes many other blade templates, all included in web and controller in this exact format and working perfectly, but the settings pages always seem to return an error 404 and I cannot figure out what is preventing them from being correctly implemented into the framework.
Please help and thank you so much
I have a problem in my starting codes in Laravel,
it's my error :
Error
Call to undefined function App\Http\Controllers\veiw()
it's my web.php (routes page) :
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/users', 'UserController#user');
and it's my Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function user() {
return veiw('index');
}
}
Hope you help me ...
You mistyped view: It is view, not veiw
I am trying different methods to resolve it for 5 hours but can't may any method work.
This is my web.php file:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'LaptopController#show');
This is Laptop.php(Model):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Laptop extends Model
{
//
}
This is LaptopController.php (Controller):
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use app\Laptop;
class LaptopController extends Controller
{
public function show(){
$laptop=Laptop::all();
echo $laptop->name;
}
}
Inside the controller you are using
use app\Laptop;
but instead it should be
use App\Laptop;
In addition to this there is the problem that you are trying to access name of a Collection: if you want just the first one you should do Laptop::first()
I can not test the api with phpunit testing. I have created a testcase and a test route. But it gives me an error like Expected status code 200 but received 405.
My User controller test:-
<?php
namespace Tests\Feature\API\Medical\V1;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\User;
class UserControllerTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testLogin()
{
$user = User::find(1);
$response = $this->actingAs($user,'api')->json('POST','/api/test');
$response->assertStatus(200);
}
}
I have not done anything to testCase base class.
My api routes:-
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('/test',function(){
return response()->json([
'success'=>true
]);
});
But this route is working on postman. When i change http method to GET it gives me my default route on routes/web.php. How to get my actual json to my testcase? thanks in advance.
I faced the same problem.
The project was raised in docker. In the .env file, you need to write:
APP_URL=http://localhost
I have basic controller, where I want it to receive any json request. Am new to api routing. I get Sorry No Page Found When I use POST MAN. First I tested it on GET and made it call a simple return but throws the error."Sorry, the page you are looking for could not be found."
I removed the api prefix in the RouteServiceProvider.php and to no success.I put my demo controller
Routing api.php
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::get('/test_api/v1', 'TestController#formCheck');
TestController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function formCheck(){
return "YES !!!";
}
public function formPost(Request $request)
{
$formData = $request->all();
return response()->json($formData, 201);
}
}
In the app/Providers/RouteServiceProvider.php.
Remove prefix('api') & it shuld look like this.
protected function mapApiRoutes()
{
Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}