I am trying to create a route:
Route::get('/apply/submit', 'ApplyController#submit');
But I keep getting the standard Laravel error page.
My ApplyController:
class ApplyController extends BaseController {
public function index() {
return View::make('apply.apply', array('metaTitle' => 'China Aupair | Internships | Apply Online'));
}
public function submit() {
return 'yay!';
}
}
Which I don't understand because Route::get('/apply', 'ApplyController#index'); works as it should.
What am I doing wrong?
I think the problem is the method you access this page. You probably try to send form (using POST method) and you use get for the route. What you should do is change:
Route::get('/apply/submit', 'ApplyController#submit');
into
Route::post('/apply/submit', 'ApplyController#submit');
because you probably send a form and not run this route manually in browser using http://localhost/yourproject/apply/submit
Related
Below is the code in my route
Route::get('patients/{$patient}/','PatientController#show');
The above will not work in my browser , it will say 404 | not found with the url
below http://127.0.0.1:8000/patients/3
But
if I change my route to code below
Route::get('patients','PatientController#show');
I will not get any error , infact it will display the view for me.
What could be the problem? I am using Laravel 5.8
You have a typo in your route, {$patient} should be {patient}.
Route::get('patients/{patient}/','PatientController#show');
Then using model-route-binding, you will have the $patient model ready for you in your show() method,
public function show(Patient $patient){
return view('patients.show', ['patient' => '$patient']);
}
This is your route problem.
Route::get('/patients/{patient?}', 'PatientController#show');
You can use url like this
http://127.0.0.1:8000/patients/3
or
http://127.0.0.1:8000/patients
And your controller is also simple
public function show(Patient $patient=null){
return view('patients.show',compact('patient'));
}
I'm trying to execute the method but it does not working. I know it is very basic and may be ask more time, but i didn't resolve.
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class LockController extends Controller {
/**
* Show the profile for the given user.
*
* #param int $id
* #return Response
*/
public function index()
{
return view('lock');
}
public function login()
{
return view('login');
}
}
?>
Route
Route::get('/lock', 'LockController#index');
Route::get('/lock', 'LockController#login');
This Route::get('/lock', 'LockController#index'); route working fine using this http://localhost/laravelDev/public/index.php/lock url but another route does not working properly and i'm getting this error NotFoundHttpException in RouteCollection.php line 161:. For login method i'm using this url http://localhost/laravelDev/public/index.php/lock/login.
I searched about it, i follow the instruction of this accepted answer but it does not work, can any one guide me where i'm wrong.
You can mention the required methods which will be handled in controller file(s) in routes.php file.
Example:
Route::get('url/of/the/resource', 'controllername#action'); (o handle 'GET' requests).
Route::post('url/of/the/resource', 'controllername#action'); (For 'POST' requests).
Details:
Route::get('/user/register', 'UserController#showRegistrationForm');
This indicates to show a registration from when given url (When GET request is made) is given like below:.
http://localhost/your_laravel_project_folder/user/register
Route::post('/user/register', 'UserController#handleUserRegistration');
This indicates to handle the registration from data when user submits the registration form. (When POST request is made).
class UserController extends Controller
{
public function showRegistrationForm()
{
return view('User.register');
}
public function handleUserRegistration()
{
$registerInput = Input::all();
var_dump($registerInput);
}
}
You have two identicals routes for different methods? I don't think that should work.
I could work if one was get and the other post.
Also, usualy, the url is would be http://localhost/lock or http://localhost/lock/login. The public/index.php shouldn't be necessaire.
Change the second route to this, pretty sure it'll work:
Route::get('/lock/login', 'LockController#login');
I am trying to redirect to PostsController#store but the page redirects to PostsController#index:
In routes.php:
Route::Resource('posts', 'PostsController');
Route::Resource('reviews', 'ReviewsController');
ReviewsController
class ReviewsController extends Controller {
public function store(Request $request) {
// (do a bunch of stuff)
return redirect(action('PostsController#store',[$request]));
}
}
PostsController
class PostsController extends Controller {
public function index() {
dd('Incorrectly redirects here');
}
public function store(Request $request, Post $post) {
dd('This is where I am trying to redirect to');
}
}
No error or exception occurs. However, once redirected, the url is:
http://localhost/laravel2devel/public/posts?POST%20/laravel2devel/public/reviews%20HTTP/1.1%0D%0AAccept:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8%0D%0AAccept-Encoding:%20%20%20%20%20%20%20%20%20%20%20gzip,%20deflate%0D%0AAccept-Language:%20%20%20%20%20%20%20%20%20%20%20en-US,en;q=0.8%0D%0ACache-Control:%20%20%20%20%20%20%20%20%20%20%20%20%20max-age=0%0D%0AConnection:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20keep-alive%0D%0AContent-Length:%20%20%20%20%20%20%20%20%20%20%20%20132%0D%0AContent-Type:%20%20%20%20%20%20%20%20%20%20%20%20%20%20application/x-www-form-urlencoded%0D%0ACookie:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20XSRF-TOKEN=eyJpdiI6InhzN2JiK0FvYnIrUnVoeGZkcGNHOXc9PSIsInZhbHVlIjoiMFdrdnluNTdrR3l3OTlrZE9QSDA1WVwvalNoeDhHbG0wUXlvT0NBblRyWDNocFwvMExCZ0dqdVppbjR2M29SdnRmbWRDMkdRc042XC9ib3hrd2xZa1JCTmc9PSIsIm1hYyI6IjQ0ZmM4YTg4YmIxNTliMGY0MzI0OGMxMjMyZGM0ZDU4ZGM4MTVlMGM2NzVmZWNmM2YzZjI5YWU4OTJhOWM5MGYifQ%3D%3D;%20laravel_session=eyJpdiI6IjRCRThcLzdaR3ZaUUZJdTVQcmtVZk5BPT0iLCJ2YWx1ZSI6IlJYTlFaWk5WZEZQbHdkQzhtalhGbko0dnUzdzN0UjhOM2FLZUJQMkloNkMwdGRjc3VcL3lNUjBaXC9acktrUkxvRzZEWW4rVlY3Q0o2alB3VnJnNEZDdnc9PSIsIm1hYyI6ImIwMjRlMzIzYWYxNDI5NjEzYmFmZjljMjg4MmFkYzU1MTkzZDVkOWRjM2IwMzZlNTM1MTE2ZWExYTA3NmYyNzgifQ%3D%3D%0D%0AHost:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20localhost%0D%0AOrigin:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://localhost%0D%0AReferer:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://localhost/laravel2devel/public/swords/1%0D%0AUpgrade-Insecure-Requests:%201%0D%0AUser-Agent:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Mozilla/5.0%20%28Windows%20NT%206.1;%20WOW64%29%20AppleWebKit/537.36%20%28KHTML,%20like%20Gecko%29%20Chrome/48.0.2564.97%20Safari/537.36%0D%0A%0D%0A_token=DeQeCCA8e19IIsxGhouybXiqIFoAMLsQ6sgp5EMF&post_title=asdfdas&user_id=2&reviewable_id=1&reviewable_type=item&post_body=asdfdfas
If you use resource controller then, store() method is expected to be called with HTTP POST. If you redirect then it will use GET, so Laravel will call index() instead. See Laravel HTTP Resource Controllers documentation.
When you add a route as a resource it will handle the Restful requests to each methods.
A get request to the url will execute the index() method. For the store() method to execute a post request need to be given. store() method is to add the resource.
Please go through the resource controller documentation.
Im not clear about your question but my idea is if you wants store reviews for particular post just use your routes like this
Route::Resource('posts', 'PostsController');
Route::Resource('posts.reviews', 'ReviewsController');
then check the route:list then you will get the list of routes, i hope you have two tables then use foreign key you can archive your goal
i hope this is good tutorial for you LINK
I have a Controller witch contains more than 150 functions. almost all of the functions inside the controller are returning views.
each view should change if the user is logged in.
I almost handled this in the view side by using parent blades and etc. But in the controller side for almost every function i should check if the user is logged in and return some specific data about the user along with the view.
for example:
$user=[];
if(Auth::check())
{
$user=Auth::user;
$reputation=$user['reputation'];
$messages=$user->messages();
$notifications=$user->notification();
}
return view('pages.profile')->with(['user'=>$user , 'messages'=>$messages , 'notifications'=>$notifications]);
I thought that this is possible using middlewares but i could not figure it out.
what are the alternatives here?
for more information i am using laravel 5.
One solution could be using your custom function instead of view(). Sth like:
//in your controller action
public function someAction() {
return $this->view('pages.profile');
}
protected function view($template, $data = array()) {
return view($template)->with($data)->with([data that needs to be passed to all views]);
}
I am using Laravel 5 and have declared a route that corresponds to a controller action but it gives an error of "This webpage has a redirect loop" and net::ERR_TOO_MANY_REDIRECTS in case of ajax calls.
My route is:
Route::get('getsubcategories/{id}', 'HomeController#getsubcategories')
->where('id', '[0-9]+');
Controller:
public function getsubcategories($id){
return "abc";
}
I don't know where the problem resides. Any suggestions ?
If you are using Form Request, make sure you are not injecting the class in the constructor.
My mistake was this:
public function __construct(SupplierFormRequest $supplier)
{
$this->supplier = $supplier;
}
instead of injecting the interface there.