I am very new to this laravel framework.
And I am stuck here. I got an error like 'Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
Error 404
localhost
Apache/2.4.33 (Win32) OpenSSL/1.0.2o PHP/5.6.36'
that's my form:i am just trying to insert data in database
#extends('layouts.app')
#section('content')
<form method="posts" action="/posts">
<input type="text" name="title" placeholder="enter the title">
<input type="submit" name="submit">
</form>
#stop
</body>
</html>
and my controller:
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller
{
public function index() {
return "lets see whether it is working or not".$id;
}
}
public function create() {
return view('posts.create');
}
public function show(){
//return view();
}
public function edit(){
//
}
public function update(Request $Request,$id){
//
}
public function destroy($id){
//
}
public function store(Request $request){
Post::create($request->all());
}
}
and finally my route:
Route::resource('posts','PostController');
Change the method to 'post' and the action to {{ route('posts.store') }}. Also be sure to add the #csrf blade directive in your from to include the csrf token.
So your form will look like:
<form method="post" action="{{ route('posts.store') }}">
#csrf
<input type="text" name="title" placeholder="enter the title">
<input type="submit" name="submit">
</form>
Related
I'm setting up my custom authentication system in my Laravel app. I've deleted all the default auth controllers and not using make::auth. And my auth is working properly. My main problem is that when I tried to log in for the first time, it's failing with "Route [login] not defined" error, but in second attempt, it's working properly. And if I repeat the process, it's continuing again and again like the first two attempt. Actually, I've never used login route anywhere.
Here is my form:
<form action="{{ url('/log-in') }}" method="POST">
#csrf
<input type="text" name="phone" placeholder="Telefon" class="form-control input-phone">
<input type="password" name="password" placeholder="Parol" class="form-control">
<button type="submit" class="btn">Kirish</button>
</form>
Here is my route:
Route::post('/log-in', 'AuthController#login');
Here is my controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use App\User;
class AuthController extends Controller
{
public function login(Request $request) {
// Get current user.
$user = User::where('phone', $request->phone)
->first();
if ( Hash::check($request->password, $user['password']) ) {
Auth::login($user, true);
Auth::logoutOtherDevices($request->password);
return redirect()->back();
}
}
}
Use for route.
Route::post('/log-in', 'AuthController#login')->name('login');
Use your form.
<form action="{{ route('login') }}" method="POST">
#csrf
<input type="text" name="phone" placeholder="Telefon" class="form-control input-phone">
<input type="password" name="password" placeholder="Parol" class="form-control">
<button type="submit" class="btn">Kirish</button>
</form>
This is a tricky and annoying problem. you need to change your return from return redirect()->back(); to loading 1 known blade or a known redirect. Sometimes at login your route fails to set a back redirect. so you can try to set a return view() or a return to a known url. So for example if the login is success return to index , if not load error page.
Hope this helps
login.php (controller)
class login extends CI_Controller {
public function index()
{
$this->load->view('login');
}
public function Click()
{
$action = $this->input->post('register'); // $_POST['start']; also works.
if($action)
{
$this->load->view('register');
}
}
}
login.php(views)
<form action="" class="loginForm" method="POST">
<div class="input-group">
<input type="submit" id="submit" class="form-control" value="Login" name="login">
<input type="submit" id="submit" class="form-control" value="Buat Akun" name="register" >
</div>
</form>
how can I change my view to register.php after clicking register button. The error is I keep back to login page after clicking register.
You can try this :
In Login controller :
public function register()
{
$this->load->view('register');
}
In view:
<form action="" class="loginForm" method="POST">
<div class="input-group">
<input type="submit" id="submit" class="form-control" value="Login" name="login">
<a href="<?php echo base_url('login/register');?>" id="submit" class="form-control" name="register" >Register</a>
</div>
</form>
You need to enable mod_rewrite module in apache
Please follow below mention step to enable mod_rewrite module:
1) Find the “httpd.conf” file under the “conf” folder inside the Apache’s installation folder.
2) Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.You can do this easily by searching the keyword “mod_rewrite” from find menu.
3) Remove the “#” at the starting of the line, “#” represents that line is commented.
4) Now restart the apache server.
5) You can see now “mod_rewrite” in the Loaded Module section while doing “phpinfo()”.
You can try this :
In Login Controller:
class Login extends CI_Controller {
public function index()
{
if($this->input->post('login') == 'Login'){
$this->load->view('login');
}else{
$this->register();
}
}
public function register(){
$this->load->view('register');
}
}
In View :
Maybe it's because I'm tired, but I can't seem to get a simple upload working for one of my models.
On the show details page of my customers (who are NOT users) model, I have a simple form where a user can upload the logo of the customer.
The form:
<form enctype="multipart/form-data" action="/customers/i/{{$customer->url_string}}" method="POST">
<input type="file" name="logoUpload">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<input type="submit" class="pull-right btn btn-sm btn-primary" value="Upload">
</form>
The Controller:
public function logoUpload(Request $request){
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
$customer->car_logo = $path;
$customer->save();
return back();
}
}
I know the issue is that I haven't defined $customer in the controller since the file does actually store itself in the correct folder after I click submit, but it does not hit the database at all.
Update
The current customer details url:
http://localhost:8000/customers/i/dsdado9a98w78721
The web definition for the post route:
Route::post('/customers/i/{customer}', 'CustomerController#logoUpload');
You have to create a hidden field in your form that contains the customer id and then use it in your controller to update it with the new file path, here is an example:
View
<form enctype="multipart/form-data" action="/customers/i/{{$customer->url_string}}" method="POST">
<input type="file" name="logoUpload">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<!-- customer_id field -->
<input type="hidden" name="customer_id" value="{{$customer->id}}">
<input type="submit" class="pull-right btn btn-sm btn-primary" value="Upload">
</form>
Controller
public function logoUpload(Request $request){
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
// get customer
$customer = Customer::find($request->customer_id);
$customer->car_logo = $path;
$customer->save();
return back();
}
}
You have some options, here is a simple one, with only adjusting that controller method:
public function logoUpload(Request $request, $customer)
{
$customer = Customer::where('url_string', $customer)->firstOrFail();
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
$customer->car_logo = $path;
$customer->save();
return back();
}
...
}
We have added a parameter to the method signature to accept the route parameter. We then find the model via url_string matching that parameter.
You also could setup route model binding as well to do the resolving of that model based on the route parameter for you.
A few days ago I started to learn laravel on laracasts and I have a small problem.
When I submit a form data using post method and want to access this data in my controller, I have an empty value.
Form:
<form method="POST" action="{{ url('/costs')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="cost-title">Cost title</label>
<input type="text" class="form-control" id="cost-title" name="title" placeholder="Enter cost title">
</div>
<div class="form-group">
<label for="cost-price">Price</label>
<input type="text" class="form-control" id="cost-price" name="price" placeholder="Enter price">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Controller:
<?php
namespace App\Http\Controllers;
use App\Cost;
class CostsController extends Controller
{
public function index(){
$costs = Cost::all();
return view('costs.index', compact('costs'));
}
public function show(Cost $cost){
return view('costs.show', compact('cost'));
}
public function create(Cost $cost){
return view('costs.create');
}
public function store(){
dd(request()->all);
}
}
In my routes I'm triggering #store using post method:
Route::get('/costs', 'CostsController#index');
Route::get('/cost/{cost}', 'CostsController#show');
Route::get('/costs/create', 'CostsController#create');
Route::post('/costs', 'CostsController#store');
And when I try to dump and die the requests:
public function store(){
dd(request()->all);
}
I have a null value. Can you please explain me why I can't see any data here?
Update your store method and pass a Request parameter like this:
public function store (Request $request)
{
$input = $request->all();
dd($input);
}
Update:
The Illuminate\Http\Request
instance provides a variety of methods for examining the HTTP request for your application and extends the
Symfony\Component\HttpFoundation\Request
If your controller method is also expecting input from a route parameter you should list your route parameters after your other dependencies. For example, if your route is defined like so:
Route::put('user/{id}', 'UserController#update');
You may still type-hint the Illuminate\Http\Request and access your route parameter id by defining your controller method as follows:
public function update(Request $request, $id)
{
//
}
So, in simple words for getting form parameters you have to pass a variable ($request) to your controller so that it can be accessible (lets just say that laravel store these form parameters in the $request variable, so you can easily pass it to controller or not).
Hope it will clear up things :)
When I login, it gives me "error page not found". It's not loading the controller function.
This is my view file:
<form name="loginform" action="welcome/login" method="post">
Enter User Name:
<input type="text" name="uname">
Enter Password
<input type="password" name="pass">
<input type="submit" name="login" value="Login">
This is my login controller file:
class Login extends Controller {
function Login()
{
parent::Controller();
}
function login()
{
$this->load->view('welcome_message');
}
}
This is my welcome controller file:
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->model('Loginmodel');
$this->load->view('welcome_message');
}
public function login()
{
$this->load->model('Loginmodel','login');
$info = $this->logn->login();
$this->load->view('welcome_message',$info);
}
}
Can you give me any idea what is wrong?
The code you provided seems fine, so I would guess it has to do with this:
<form name="loginform" action="welcome/login" method="post">
The path is relative, which is only going to work if there are no url segments present. If the current URL is /hello/world, you will end up posting to /hello/world/welcome/login
Try a full URL or absolute path. Example:
<form name="loginform" action="/welcome/login" method="post">
<!-- ^^^ Note the leading forward slash -->
Or use base_url() for the full url:
<form action="<?php echo base_url(); ?>welcome/login">
You may also be interested in the form_open() function which will do this automatically.
<?php echo form_open('welcome/login'); ?>
The only other thing I can see would have to do with whatever $this->login->login(); does. If you can confirm what your URL actually is when you see the "page not found" error, that would be a great help.