Laravel route doesn't work - php

I am making basic route with controller but it doesnt work, I dont know why... Earyer worked for me but now doesnt...
Here is routes.php:
Route::get('/admin', array(
'as' => 'admin-login',
'uses' => 'AdminController#getAdminLogin'
));
Here is AdminController.php:
class AdminController extends BaseController {
public function getAdminLogin()
{
return View::make('admin.index');
}
}
When I type localhost/project/public/admin... It goes to localhost/admin
Why?

Why don't you use Laravel's own Web Server:
php artisan serve
And then, try this:
http://localhost:8000/admin/

Related

Laravel 7 Auth not working on hosted site

So I am trying to deploy a Laravel site to bluehost however after successful login which should redirect to '/home' and stop. It instead tries to redirect to '/home' then redirects to '/login'. the same code running on localhost with the same database works fine. Other database operations work fine.
Basically the default auth middleware seems to broken somehow.
I used laravel's built in auth to make make the authentication.
here is the live site:
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
Route::get('/', 'WelcomeController#index')->name('welcome');
Auth::routes();
// Home
Route::get('/home', 'HomeController#index')->name('home');
//Listings
Route::get('/listings/search', 'ListingController#search')->name('search-listings');
Route::get('/listings/delete/{listing}', 'ListingController#destroy')->name('listing-delete');
Route::resource('listings', 'ListingController');
Route::group(['prefix' => 'messages'], function () {
Route::get('/', ['as' => 'messages', 'uses' => 'MessageController#index']);
Route::get('create', ['as' => 'messages.create', 'uses' => 'MessageController#create']);
Route::post('/', ['as' => 'messages.store', 'uses' => 'MessageController#store']);
Route::get('{id}', ['as' => 'messages.show', 'uses' => 'MessageController#show']);
Route::put('/', ['as' => 'messages.update', 'uses' => 'MessageController#update']);
Route::delete('/', ['as' => 'messages.action', 'uses' => 'MessageController#action']);
});
So it turns out there was a new line before <?php in one of my files in app/config !!
Almost drove me crazy. Worked fine on localhost but not on server. If you encounter this issue, check your .php files for new lines at the beginning of the file.
For me, deleting the files in /bootstrap/cache folder except the .gitignore
then running the command composer dumpautoload worked

laravel cant find method in live server

i'm facing a weird error in my machine locally everything is working properly while when i throw the project in server live i have this problem BadMethodCallException which says cant find the method but the method actually it exist, i tried everything php artisan key:generate, composer dump-autoload, php artisan cache:config but with no success.
my route:
Route::get('/forgotpassword',[
'as' => "forgotpassword",
'uses' => "admin#forgotpassword"
]);
controller:
//Forgot Password
public function forgotpassword()
{
return view('page.forgotpassword');
}
And error i'm getting:
BadMethodCallException
Method App\Http\Controllers\admin::forgotpassword does not exist.
Try this:
Route::get('/forgotpassword',[
'as' => "forgotpassword",
'uses' => "Admin#forgotpassword"
]);
Remember: All class name is case-sensitive. XAMPP corrected this automatically.
usually this happens when you develop in windows but host it on Linux.
In Linux we have to also take care of the case sensitivity.
make sure your class name is same as you are referencing in your routes.
e.g
Route::get('/forgotpassword',[
'as' => "forgotpassword",
'uses' => "admin#forgotpassword"
]);
for the above given route you must have a controller name with lower case.
e.g
class admin extends Controller {}
Hope this helps
namespace App\Http\Controllers;
use App\Http\Controllers;
class Admin extends Controller {
}
Route
Route::get('/forgotpassword',[
'as' => "forgotpassword",
'uses' => "Admin#forgotpassword"
]);

Laravel : BadMethodCallException Method [show] does not exist

(1/1) BadMethodCallException
Method [show] does not exist. in Controller.php (line 82)
I am new to Laravel and PHP and have been stuck on this error for a very long time with other questions not providing a solution. I was following an example (where the example worked) and made very little changes beside name changes.
Here is the code:
web.php file
Route::get('/', 'PagesController#home');
Route::get('faq', 'PagesController#faq');
Route::resource('support', 'UserInfoController');
UserInfoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\UserInfo;
class UserInfoController extends Controller
{
//
public function create(){
$userInfo = new UserInfo;
return view('contact', ['userInfo' => $userInfo]);
}
public function store(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'subject' => 'required',
'description' => 'required',
]);
UserInfo::create($request->all());
return redirect()->route('contact')->with('success','Enquiry has been
submitted successfully');
}
}
UserInfo.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserInfo extends Model {
protected $fillable = [
'name','email','subject','description',
];
}
The Route::resource is the one giving me the problem as I am trying to access the page support/contact. Would be very grateful if someone knew how to solve this.
That is because you are doing resource routes in your routes.php file that generates all the routes for the CRUD functions when you have to generate a route for the show method you find that it does not exist.
To solve it only creates the methods that you ask or, also you can define only the routes that you need.
The controller is trying to invoke the 'show' method - which you should have defined if you're going to load /support/{id} via GET in your browser. You can see the expected methods for a resource here:
https://laravel.com/docs/5.4/controllers#resource-controllers
You can also make your life somewhat easier by starting with a valid controller by using the built in generator:
php artisan make:controller UserInfoController --resource
If you don't want to supply ALL the methods, you have to specify, for example:
Route::resource('support', 'UserInfoController', ['only' => [
'create', 'store'
]]);
Have you added method Show to your Controller ? Route::Resource has 7 basic routes:
Verb Path Action Route Name
GET /support index support.index
GET /support/create create support.create
POST /support store support.store
GET /support/{support} show support.show
GET /support/{support}/edit edit support.edit
PUT /support/{support} update support.update
DELETE /support/{support} destroy support.destroy
As you see there is a route called show which will be default when you route to support so you must connect this route to it's method in the controller which is in resource case CONTROLLER/show, however in your case you're trying to get a static page from a prefix called support which is different from resources because show in resource handling dynamic results.
Use this syntax to get a page called contact from prefix called support
Route::prefix('support')->group(function () {
Route::get('contact', function () {
// Matches The "/UserInfoController/contact" URL
});
});

Laravel 5.1 - How to set the loginPath in AuthController?

I want to change the default Laravel routes from /auth/login to just /login and vis-à-vis with register.
These are my routes:
Route::get('login', ['as' => 'getLogin', 'uses' => 'Auth\AuthController#getLogin']);
Route::post('login', ['as' => 'postLogin', 'uses' => 'Auth\AuthController#postLogin']);
Route::get('logout', ['as' => 'getLogout', 'uses' => 'Auth\AuthController#getLogout']);
Route::get('register', ['as' => 'getRegister', 'uses' => 'Auth\AuthController#getRegister']);
Route::post('register', ['as' => 'postRegister', 'uses' => 'Auth\AuthController#postRegister']);
Now the problem arises, that when a user tries to access an area that is guarded, the Authentication class kicks in, redirecting the unauthenticated user back to /auth/login instead of just /login.
I though I could solve the problem by setting the $loginPath in my AuthController like this:
protected $loginPath = '/login';
But it seems like the $loginPath is just used for unsuccessful login attempts, instead of unsuccessful authentication attempts, like documented in the AuthenticateUsers Class.
Well I managed to change the redirectURL in the Authenticate Class from this:
return redirect()->guest('auth/login');
to this:
return redirect()->guest('login');
Which solved the issue, yet I would like to set a property for this in my AuthController like this:
protected $redirectIfMiddlewareBlocks = '/login';
For this I check in the Authenticate Class if a property exists, which I set in the AuthController before:
return redirect()->guest(property_exists($this, 'redirectIfMiddlewareBlocks') ? $this->redirectIfMiddlewareBlocks : '/shouldnotbeused');
Yet I get the /shouldnotbeused url, instead of the one set by the redirectIfMiddlewareBlocks property in my AuthController.
How do I set up the path for the login route in my AuthController correctly?
I think the problem is that you are checking the property on the wrong class. You are checking if it exists on $this, which is an instance of Authenticate when it is set on AuthController
So you should change your check to:
property_exists(AuthController::class, 'redirectIfMiddlewareBlocks')
Also, you can't access a protected property from another class. If you want to do that you'll have to make it public. Finally, I would make it static so that you don't have to instantiate an AuthController object to access it. So the final solution is:
class AuthController {
public static $redirectIfMiddlewareBlocks = '/here';
}
And in your Authenticate middleware:
use App\Http\Controllers\AuthController;
class Authenticate {
public function handle() {
return redirect()->guest(
property_exists(AuthController::class, 'redirectIfMiddlewareBlocks')
? AuthController::$redirectIfMiddlewareBlocks
: '/shouldnotbeused'
);
}
}

Laravel 5 can't find class for custom namespace

In my Laravel app I'm splitting the front end and back end code into folder. These are app/Http/Controllers/BackEnd and app/Http/Controllers/FrontEnd. Rather than type all that out on every file I thought it would be easier to define two namespaces, BackEnd and FrontEnd. I've edited my composer file to this:
"autoload": {
"classmap": [
"app/Models",
"database"
],
"psr-4": {
"App\\": "app/",
"BackEnd\\": "app/Http/Controllers/BackEnd",
"FrontEnd\\": "app/Http/Controllers/FrontEnd"
}
},
I then ran composer autodump and set up my route file like this:
Route::group(['prefix' => 'webman', 'middleware' => 'auth', 'namespace' => 'BackEnd'], function()
{
Route::get('/', ['as' => 'webmanHome', 'uses' => 'HomeController#index']);
});
But when I browse to localhost:8000/webman/ I just get an error, Class App\Http\Controllers\BackEnd\HomeController does not exist. The controller does exist, this is the file:
<?php namespace BackEnd;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class HomeController extends Controller {
/**
* Display the admin home page, showing the front-end menu and "apps" as links to other sections of the ACP.
*
* #param Reqeust $request
*
* #return View
*/
public function index(Request $request)
{
return view('backend.index');
}
}
I've checked vendor/composer/autoload_psr4.php to make sure that the namespace is being defined and it is, this is in the array returned 'BackEnd\\' => array($baseDir . '/app/Http/Controllers/BackEnd'),.
Strangely if I use <?php namespace App\Http\Controllers\BackEnd; at the top of HomeController.php then everything works, why? What am I missing? Why can't autoload find the file with just BackEnd?
When setting namespace in Route::group() it is actually appending that to App\Http\Controllers. What you could do is remove it and reference the full path like so:
Route::group(['prefix' => 'webman', 'middleware' => 'auth'], function()
{
Route::get('/', ['as' => 'webmanHome', 'uses' => '\BackEnd\HomeController#index']);
});
Try changing/commenting the below line in RouteServiceProvider.php
protected $namespace = 'App\Http\Controllers';
There's an interesting and easy way to get around this... Service Providers.
When the route file is loaded via a provider, the 'App\Http...' is not enforced.
public function boot()
{
$this->loadRoutesFrom(app_path('Your/Model/routes.php'));
}
Keep in mind no middleware is attached either. Your route group will have to specify a 'web' middleware or you'll go nuts wondering why validation, etc, isn't working anymore....(been there!)
It's a cool way to go about it anyway, using providers leads to more modular code and re-use.

Categories