I'm creating a contact form in Laravel 5. The contact form is on homepage and when a user fills it out then clicks the submit button, laravel sends the email using mandrill and just refreshes the page. The code works perfectly on my Local environment. However on digital ocean droplet when a user clicks the submit button, domain.com/contact route is being triggered and I can only see a blank page.
Here is my controller
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ContactFormRequest;
use Illuminate\Http\Request;
class ContactController extends Controller {
public function create() {
return view('forms.contact');
}
public function store(ContactFormRequest $request) {
\Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'user_message' => $request->get('message')
), function($message)
{
$message->from('info#pbl-landing.com');
$message->to('muhammetergenc#gmail.com', 'Admin')->subject('Contact Request');
});
return \Redirect::route('index')->with('message', 'Thanks for contacting us!');
}
}
Here is my routes.php file
Route::get('/',
['as'=>'index', 'uses'=>'WelcomeController#index']);
Route::get('home', 'HomeController#index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Route::get('contact',
['as' => 'contact', 'uses' => 'ContactController#create']);
Route::post('contact',
['as' => 'contact_store', 'uses' => 'ContactController#store']);
Related
I can't understand what is happening here. on my local server its working fine but when I put everything on my live server I'm having this error
My web Route
Auth::routes(['verify' => true]);
Route::get('/', function(){
return view('auth.login');
})->name('auth.login')->middleware('auth');
Route::resource('/register', 'Auth\registerController#index')->name('register');
Route::group( ['middleware' => 'auth' ], function()
{
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['namespace' => 'dashboard', 'prefix' => 'dashboard'], function() {
Route::get('/', 'DashboardController#index');
});
SO ON...
}
My RegisterController
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
public function index(Request $request)
{
$referral = '';
$keyword = $request->get('search');
$referral = Referral::where([
['code', $keyword],
['status', 0]
])->first();
if (is_null($keyword))
return view ( 'Auth.register');
elseif ($referral)
return view ( 'Auth.register', compact('referral', $referral))
->withDetails ( $referral )
->withQuery ( $keyword );
else
return view ( 'Auth.register')->withMessage ( 'The code you provided is not existing or already been taken.' );
}
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'country_code' => $data['country_code'],
'phone_number' => $data['phone_number'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
registerController Path
what do you this causing this?
Thank you in advance!
this line of code in your web.php file is causing the issue
Route::resource('/register', 'Auth\registerController#index')->name('register');
your class name starts with R but you have written using r. moreover it seems the route is a simple get route but you have written it as resource. change this too. so the route should be like
Route::get('register', 'Auth\RegisterController#index')->name('register');
How did you namespace your registerController?
Because laravel later versions use a rather different namespace than previous versions. Hence change your namespace (and imported files) to this;
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
If you are using laravel 8:
use App\Http\Controllers\Auth\RegisterController;
Auth::routes(['register' => false]);
Route::get('/user_register', [RegisterController::class, 'showRegistrationForm'])->name('register');
However it throws this error when I clean cache:
Unable to prepare route [user_register] for serialization. Another route has already been assigned name [register].
The new route works, but the old route yet it works
I am using using Laravel Spatie for User Permission for my api. The version is Laravel-5.8.
OrderController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Order;
use App\User;
use App\Student;
use App\Guardian;
class OrderController extends Controller
{
public function index(Request $request)
{
if(!Auth::user()->hasPermissionTo('View Order'))
return response()->json([ "message" => 'User do not have permission'], 401);
return response()->json(::with('order_detail')->get(), 200);
}
public function store(Request $request)
{
if(!Auth::user()->hasPermissionTo('Add Order'))
return response()->json([ "message" => 'User do not have permission'], 401);
$request->validate([
'week_day' => 'required|integer',
'start' => 'required',
'end' => 'required',
'amount' => 'required',
'full_name' => 'required'
]);
$orders = Order::create($request->all());
return json_encode($orders);
}
public function show($id)
{
if(!Auth::user()->hasPermissionTo('View Order'))
return response()->json([ "message" => 'User do not have permission'], 401);
return json_encode(Order::findOrFail($id));
}
}
api.php
Route::group([
'middleware' => 'auth:api'
], function () {
Route::get('order/mobile', 'OrderController#mobile');
Route::resource('order', 'OrderController', ['only' => ['index', 'show', 'store', 'update', 'destroy']]);
});
I want anyone that get to the site to be able to create, store and send order without the need to login. However, permission is needed for index, update and delete. There should not be permission for that. How do I adjust my controller and api.php to achieve this expected result to create and store order?
auth middleware on your route group is preventing all unauthenticated users from accessing them. You can either remove the middleware or create two groups where index, update, and delete is under auth middleware and the second group without auth for other routes.
If you remove the middleware from the route group you can add it to specific methods in your controller like so
public function __construct()
{
$this->middleware('auth')->only(['index', 'update', 'destroy']);
}
and of cource you can also specify spatie middleware
public function __construct()
{
$this->middleware('role:Contributor')->only(['index', 'update', 'destroy']);
}
However, you can also manualy test within your controller methods for various user permissiosn
if ($user->hasRole('Contributor')){};
This way you can allow unauthenticated users to perform some actions but restrict some actions to auth users.
I'm learning laravel and I have a problem with my base url which is http://localhost/.
It keeps redirecting to http://localhost/home which is the base authentication route and since im not logged in, it redirects me to http://localhost/login.
I want http://localhost/ to redirect to http://localhost/blog/posts as it should.
I'm doing this because in the future the base url will redirect to another page. Until then, I want to display the blog posts.
web.php
Route::get('/', [
'as' => 'index',
'uses' => 'HomeController#index',
]);
Route::get('/blog/posts', [
'as' => 'blog',
'uses' => 'BlogController#index'
]);
Auth::routes();
Route::get('/home', [
'as' => 'home',
'uses' => 'HomeController#home'
]);
HomeController.php
public function home()
{
return view('home');
}
public function index()
{
return view('blog');
}
I hope I was clear enough, i'd be glad to give more info if needed.
Problem solved:
Comment or remove $this->middleware('auth'); in HomeController.php and add it in the route:
Route::get('/home', [
'as' => 'home',
'uses' => 'HomeController#home'
])->middleware('auth');
Check that the HomeController __construct() function doesn't have an auth middleware inside, that would try to log you in first, then continue to check for the index function.
When the user log in or register, he is supposed to go the dashboard, but instead of it, it is being located to the /login page, which is not even there and hence error occours:
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in RouteCollection.php line 161:
Routes:
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::group(['middleware' => ['web']], function(){
Route::get('upload',function(){
return view('files.upload');
});
Route::get('/wallet',[
'uses' => 'WalletController#getwallet',
'as' => 'wallet'
]);
Route::post('/addmoney',[
'uses' => 'WalletController#addmoney',
'as' => 'addmoney'
]);
Route::post('/signup',[
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
Route::post('/signin',[
'uses' => 'UserController#postSignIn',
'as' => 'signin'
]);
Route::get('/dashboard',[
'uses' => 'UserController#getDashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);
Route::post('/handleUpload','FilesController#handleUpload');
Route::get('/pay', ['as' => 'pay', 'uses' => 'PaymentController#pay']);
# You will need one more.
Route::get('/payment/status', ['as' => 'payment_status', 'uses' => 'PaymentController#status']);
/**
* Using Named Routs to demonstrate all the possibilities.
*/
});
User controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use InvalidConfirmationCodeException;
use Flash;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class UserController extends Controller
{
public function getDashboard(){
return view('files.dashboard');
}
public function postSignUp(Request $request)
{
$this -> validate($request,[
'email' => 'required|email|unique:users',
'name' => 'required|max:20',
'password' => 'required|min:4'
]);
$email = $request['email'];
$name = $request['name'];
$password = bcrypt($request['password']);
$user = new User();
$user->email =$email;
$user->name = $name;
$user->password = $password;
$user->save();
return redirect()->route('dashboard');
//Auth::login($user);
}
public function postSignIn(Request $request)
{
$this -> validate($request,[
'email' => 'required',
'password' => 'required'
]);
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
}
WalletController
public function getwallet(){
return view('files.wallet');
}
public function addmoney(Request $request){
$this->validate($request,[
'amount'=>'required'
]);
$amount = $request['amount'];
$wallet = new Wallet();
$wallet->amount=$amount;
$wallet->save();
return redirect()->route('/addmoney');
}
Even when I try localhost:8000/dashboard , it loads as localhost:8000/login as shows same error.Also, same problem occurs when I try to load /addmoney page, when the user submit amount and redirect to the next addmoney page.
I see your dashboard view view is in file.controller but you use the auth middleware. Put your dashboard view in your dashboard view in the auth folder.
Default in Laravel: resources/views/auth/yourviewhere
I'm using Laravel 5.2 and caffeinated Modules and I'm getting this error
NotFoundHttpException in RouteCollection.php line 161:
I only get this error when I upload to my server but on my localhost I don't get an error.
I've also noticed that on my localhost I get all my routes listed but on my server I only have the home page.
My Users Module route.php
Route::group(['middleware' => 'web'], function()
{
Route::get('admin/', [
'uses' => 'UsersController#showLogin',
'as' => 'login'
]);
Route::post('admin/', [
'uses' => 'UsersController#doLogin',
'as' => 'doLogin'
]);
});
and my Users Module UsersController.php
<?php
namespace App\Modules\Users\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UsersController extends Controller
{
public function showLogin(){
echo "Users Controller";
}
}
If there is anything I've missed to help with this please let me know.
This error generally shown when you access a url which is not defined in your route file. Recheck the url you are trying to access.
Your code doesn't seem to have any special error.
Add a doLogin method in your UsersController.
Besides, you can try by removing / from the route path.
I mean change this:
Route::group(['middleware' => 'web'], function()
{
Route::get('admin/', [
'uses' => 'UsersController#showLogin',
'as' => 'login'
]);
Route::post('admin/', [
'uses' => 'UsersController#doLogin',
'as' => 'doLogin'
]);
});
to this:
Route::group(['middleware' => 'web'], function()
{
Route::get('admin', [
'uses' => 'UsersController#showLogin',
'as' => 'login'
]);
Route::post('admin', [
'uses' => 'UsersController#doLogin',
'as' => 'doLogin'
]);
});
I finally found the answer. I had to delete the /storage/app/modules.json and then I ran
php artisan module:optimize