laravel route - search form takes to default path - laravel

I have the website in a subfolder _demo (laravel app)
Here is my htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^teacode.ma$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.teacode.ma$
RewriteCond %{REQUEST_URI} !_demo/public/
RewriteRule (.*) /_demo/public/$1 [L]
all the routes works except for /courses/search route which takes me to
http://example.com/_demo/public/courses/search?term=term
Here is the form (should take to http://example.com/courses/search?term=term)
<form class="form-inline my-2 my-lg-0" action="/courses/search/" method="GET">
<input class="form-control search-input" type="search" name="term" required placeholder="Search">
<button class="btn search-btn" type="submit">
<i class="fas fa-search"></i>
</button>
</form>

I solved it , the problem was in the last '/' on the /courses/search/ , I changed it to /courses/search

Related

this form return 404 error in laravel blade.php

this is my html code
<!DOCTYPE html>
<html>
<body>
<form action="{{url('/contacts')}}" method="post">
#csrf
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
and this is my web route
Route::post('/contacts', [AdminController::class, 'contacts']);
and this is my base url in .env
APP_URL=http://localhost/addressbook/public
The easiest way to work with urls to routes in Laravel is using named routes. Add the name call to your route definition.
Route::post('/contacts', [AdminController::class, 'contacts'])->name('contacts.store');
Now you can get the url to the route like this.
<form action="{{ route('contacts.store') }}" method="post">
If you need variables to the route call, it is done like this based on the url parameter naming.
route('contacts.store', ['contact' => $contact]);
Your app url looks wrong, at least i would not expect it to have public in the url. Either change your .htaccess or have a different local setup. For what it is worth, php artisan serve can serve the files locally and just needs a SQL database, for a simple development environment.
APP_URL=http://localhost/addressbook

Laravel: something is changing the method, what is it?

I have a simple blade form mith post method and csrf token:
<form name="login" action="{{ route('login.do') }}" method="post" autocomplete="off">
#csrf
<label>
<span class="field icon-envelope">E-mail:</span>
<input type="email" name="email" required/>
</label>
<label>
<span class="field icon-unlock-alt">Password:</span>
<input type="password" name="password_check"/>
</label>
<button class="gradient gradient-orange radius icon-sign-in" type="submit">Send</button>
</form>
and the route in routes/web.php:
Route::post('login', 'AuthController#login')->name('login.do');
when I submit the form, I get the error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
As you can see, the route and the form are ok, but something is messing it up and changing the method. It won't even get to the controller layer.
some infos:
-Laravel 8.12
-I have no customized middleware
-The htaccess and config/cors are default
-The route doesn't end with a slash nor is duplicated
-Whenever I do an alteration I clean the caches
-The APP_URL starts with http and SESSION_SECURE_COOKIE is set to false due to it
-The APP_ENV is production
What can it be? I've read it may be something in the server that's blocking the request and changing its method, so I'm not sure if the error is laravel or server related.
public function login(Request $request){
dd($request->all());
}

how to set a home page in codeigniter

I am new in codeIgniter..Im trying to create an anchor tag to link my home page from home page like this
main-nav.php
<li>
<?php echo anchor('Welcome/home', 'Home', 'class="icon icon-photo"') ?>
</li>
but when I click the home button its show me a blank page in staid the main page or the same page.
in my controller
here is my code
Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->home();
}
public function home() {
$this->load->model('model_books');
$this->load->helper('html');
$this->load->helper('url');
$data['books_details'] = $this->model_books->getBookDetails();
$data['title'] = 'NMSC Online Library';
$this->load->view('welcome_message', $data);
$this->load->view('templates/main_nav');
$this->load->view('templates/header');
$this->load->view('main_body');
$this->load->view('templates/footer');
}
}
what im trying to do is i have a main page that have a button that link into own page i mean the main page. ( main page link to main page ).
I knew how to link a page in pure html like this..
Home
but in codeigniter i cant. pls help.
thanks and advance..
in my controller i have a Welcome.php
the code is shown already above.
in views folder there is a folder name templates inside of it i have a main_nav.php. the code is
<body>
<div class="container">
<!-- Push Wrapper -->
<div class="mp-pusher" id="mp-pusher">
<!-- mp-menu -->
<nav id="mp-menu" class="mp-menu">
<div class="mp-level">
<h2 class="icon icon-world">All Categories</h2>
<ul>
<li>Home
</li>
<li><a class="icon icon-photo" href="#">Staff</a </li>
<li class="icon icon-arrow-left">
<a class="icon icon-news" href="#">Events & Activities</a>
<div class="mp-level">
<h2 class="icon icon-news">Events & Activities</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li>Orientation Schedule</li>
</ul>
</div>
</li>
<li><a class="icon icon-photo" href="#">Services</a></li>
<li><a class="icon icon-photo" href="#">Linkages</a></li>
<li class="icon icon-arrow-left">
<a class="icon icon-news" href="#">Databases</a>
<div class="mp-level">
<h2 class="icon icon-news">Databases</h2>
<a class="mp-back" href="#">back</a>
<ul>
<li>Proquest</li>
<li>E-books Open Access</li>
<li>Journals Open Access</li>
<li>Low Online Resources</li>
<li>Medical & Nursing Resources</li>
<li>Thesis & Dissertations</li>
<li>News Online Resources</li>
<li>Other Online Resources</li>
</ul>
</div>
</li>
<li><a class="icon icon-photo" href="#">Service Hours</a></li>
<li><a class="icon icon-photo" href="#">WebLIS-OPAC</a></li>
<li><a class="icon icon-photo" href="#">Library System</a></li>
<li><a class="icon icon-photo" href="#">New Acquisitions</a></li>
<li><a class="icon icon-photo" href="#">The Librarian</a></li>
</ul>
</div>
</nav>
<!-- /mp-menu -->
<div class="scroller"><!-- this is for emulating position fixed of the nav -->
i wold link that homepage button to the same page..
in my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /nmsc-online-library/
# If your project is in server root then should be: RewriteBase /
# If project is in folder then it should be: RewriteBase /folder_name/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
To set Home Controller
Goto application/config/routes.php
$route['default_controller'] = "controller_name/method";
#if you define you first method inside index() then no need of entering method in second parameter.
And create link to another controller.
Home
About Us
Products
Corsair
Contact Us
If you place .htaccess file the you can remove index.php in above URL
EDIT 01
In .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
try this:
<li>
<?php echo anchor("", "Home", array("target" => "_self")); ?>
</li>
Write this in your li tag if your controller name is welcome
or if its Welcome then change it to

Unable to get any $_POST data from any form

I have tried many google hits but I am yet unable to retrieve any POST data from forms, $_POST is just an empty array array(0) { }. I found few posts which was saying that problem is in .htaccess which doesn't correctly redirect my POST data but any of those posts didn't help me. I have tried many .htaccess files I found but none of them worked with POST data.
I am using Debian Jessie with Apache 2.4.10, PHP 5.6.14-0+deb8u1. I have AllowOverride all inside <Directory at my VHOST file. CodeIgniter 3.0.3. I have var_dump($_POST); at top of the index.php for testing. I also have enabled rewrite_module. There are no errors in the apache2 error.log file.
Here is my setup:
Controller: Admin.php
<?php
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->library('session');
$this->load->library('form_validation');
}
public function index()
{
if(isset($this->session->userdata['logged']))
{
$this->load->view('admin/dashboard');
}
else
{
$this->load->view('templates/default/header');
$this->load->view('admin_login');
$this->load->view('templates/default/footer');
}
}
public function login()
{
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == FALSE)
{
echo "Validation failed!";
}
else
{
echo "Validation success!";
}
}
}
View: admin_login.php:
<form class="form-signin" method="post" action="/admin/login/">
<h2 class="form-signin-heading">Kirjaudu sisään</h2>
<label for="inputEmail" class="sr-only">Sähköpostiosoite</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Sähköpostiosoite" required autofocus>
<label for="inputPassword" class="sr-only">Salasana</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Salasana" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Muista minut
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Kirjaudu</button>
Current .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Your action is wrong. It should come like this
action="<?php echo base_url()?>admin/login/">
as well as load url in helpers
$this->load->helper('form','url');
as well as in these input tags there is no name attribute
<input type="email" id="inputEmail" class="form-control" placeholder="Sähköpostiosoite" required autofocus>
<input type="password" id="inputPassword" class="form-control" placeholder="Salasana" required>
Do like this
In controller
$this->load->helper('form'); #load library
In view
<?
$attributes = array('class' => 'form-signin');
echo form_open('admin/login/', $attributes);
$data1 = array(
'id' => 'inputEmail',
'class' => 'form-control',
'Placeholder' => 'Sähköpostiosoite',
'name' => 'email',
);
echo form_input($data1);
$data2 = array(
'id' => 'inputPassword',
'class' => 'form-control',
'Placeholder' => 'Salasana',
'name' => 'password',
);
echo form_password($data2);
echo form_submit('submit', 'Log In');
echo form_close();
Try this:
In controller add $this->load->helper(array('form','url')); in function __construct()
In View:
<form class="form-signin" method="post" action="<?php echo base_url()?>admin/login">
<h2 class="form-signin-heading">Kirjaudu sisään</h2>
<label for="inputEmail" class="sr-only">Sähköpostiosoite</label>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Sähköpostiosoite" required autofocus>
<label for="inputPassword" class="sr-only">Salasana</label>
<input type="password" name = "password" id="inputPassword" class="form-control" placeholder="Salasana" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Muista minut
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Kirjaudu</button>
Hope this help! ^^

404 error when redirection to a link codeigniter

I have hosted a site in my live server for testing.but i have used code to get to the specific controllers in my system .It is working well in my localhost but in my live server it doesnt work
Url are as follows
In local
a localhost:55/axxxx
b 69.1xx.xxx.xxx/~username/axxxx/axxxx
when calling a controller using a it works well but when calling the controller using b it gives me 404 error
here is my co`index.php/Authenticaton/authenticate">
<p>
<label for="login">Username</label>
<input type="text" name="username" placeholder="Username or email" required>
</p>
<p>
<label for="password">Password</label>
<input type="password" name='password' placeholder="Password" required>
</p>
<p>
<input type="submit" name="submit" id="login" class="btn-1" value="Continue">
</p>
</form>`
Pleas help me in this situation
Sorry for the Bad English
Thanks in Advance
Does it work while putting index.php before controller name, if so take a look at your htaccess.This might work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
might be base_url is pointing to different location.
Put this instead of base_url:
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";

Resources