So, i started with laravel. Tried with making a form post to the same page.
Here's what i have so far,
routes.php
Route::get('/', 'HomeController#showWelcome');
Route::group(array('before' => 'csrf'), function () {
Route::post('contactus', 'HomeController#sendEmail');
});
hello.php
<?php echo Form::open(array('action' => 'HomeController#sendEmail'))?>
input fields here
<?php echo Form::close() ?>
HomeController
public function showWelcome()
{
return View::make('hello');
}
public function sendEmail()
{
print_r($_POST);exit;
}
Problem: Form gets posted to the url public/contactus
Can someone point out which really stupid thing, i am doing?
routes.php
Route::get('/', 'HomeController#showWelcome');
Route::post('/', array(
'before' => 'csrf', // csrf filter
'uses' => 'HomeController#sendEmail' // the controller action to be used
));
hello.php
<?php echo Form::open(array('action' => 'HomeController#sendEmail')) ?>
<!-- input fields here -->
<?php echo Form::close() ?>
HomeController.php
Public function showWelcome()
{
return View::make('hello');
}
public function sendEmail()
{
$data = Input::all();
print_r($data);
// return the same view but with posted fields in the $data array
return View::make('hello', $data);
}
Routes
Route::get('/', 'HomeController#showWelcome');
Route::post('/', 'HomeController#sendEmail');
Hello.blade.php
#if(isset($post))
{{$post}}
#endif
{{Form::open()}}
{{Form::text('sometext')}}
{{Form::close()}}
HomeController
Public function showWelcome()
{
return View::make('hello');
}
public function sendEmail()
{
$post = Input::all();
return View::make('hello', array('post' => $post));
}
Related
I'm getting an undefined variable when trying to pass data from my controller to a view from a database.
Part of my Controller:
public function index()
{
$vaccine_feed = Vaccines::All();
return view('welcome', [
'vaccines' => $vaccines,
'user' => $user,
'vaccine_feed' => $vaccine_feed
]);
}
View:
foreach($vaccine_feed as $vaccines ) {
}
I've added my routes below, if that helps at all
Auth::routes();
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/logout', function () {
Auth::logout();
Session::flush();
return view('welcome');
});
Route::get('/addstock', 'AddForm#getStockForm');
Route::post('/addstock', 'AddForm#postStockForm');
Are you including addstock.blade.php in your welcome template. Because that view has different scope you may want to pass your $vaccine_feed to wherever you are including.
Try dd($vaccine_feed) in your controller (before the return) and see if it returning anything.
is it possible to pass id through an link href in Laravel and display that page like /projects/display/2.
I have this link:
<td>View</td>
It displays the id when hovering over the link as /projects/display/2. But whenever i click on the link i get an error message of:
Sorry, the page you are looking for could not be found.
I have a view setup called projects/display, plus routes and controller.
routes:
<?php
Route::group(['middleware' => ['web']], function (){
Route::get('/', 'PagesController#getIndex');
Route::get('/login', 'PagesController#getLogin');
Auth::routes();
Route::get('/home', 'HomeController#index');
Route::get('/projects/display', 'ProjectsController#getDisplay');
Route::resource('projects', 'ProjectsController');
});
Controller:
<?php
namespace App\Http\Controllers;
use App\project;
use App\Http\Requests;
use Illuminate\Http\Request;
use Session;
class ProjectsController extends Controller
{
public function index()
{
}
public function create()
{
return view('projects.create');
}
public function store(Request $request)
{
$this->validate($request, array(
'name' => 'required|max:200',
'description' => 'required'
));
$project = new project;
$project->name = $request->name;
$project->description = $request->description;
$project->save();
Session::flash('success', 'The project was successfully created!');
return redirect()->route('projects.show', $project->id);
}
public function show()
{
$project = Project::all();
return view('projects.show')->withProject($project);
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function getDisplay($id){
$project = Project::find($id);
return view('projects/display')->withProject($project);
}
}
You need to change your route to:
Route::get('/projects/display/{id}', 'ProjectsController#getDisplay');
And then generate URL with:
{{ url('projects/display/'.$projects->id) }}
If you write route like below,
Route::get('/projects/display/{projectId}', 'ProjectsController#getDisplay')->name('displayProject');
You can use the name 'displayProject' in the href and pass the id as Array :
<td>View</td>
What you are looking for is a parameterized route. Read more about them here:
https://laravel.com/docs/5.3/routing#required-parameters
I found a better solution:
In your blade file do like this
<a href="{{route('displayProject',"$id")}}">
View
</a>
with this route , in route file
Route::get('/projects/display/{id}', 'ProjectsController#getDisplay');
$id is sent form your controller with compact
return view('viewPage', compact('id'));
I'm using Laravel 5.3 and Auth by default with this roles package. How can i do the normal user redirection after the user login if i have similar roles and also pages for them. For example i have AdminRole and after the Login i want to redirect user to /admin/dashboard.
I have tried something like this in the LoginController but it doesn't make sense:
protected function redirectTo()
{
if (Auth::user()->isRole('admin'))
return redirect()->route('admin');
return redirect()->route('home');
}
Or maybe there is a better way to use middleware for redirecting?
Here is my routes (web.php):
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
Route::resource('company', 'CompanyController');
Route::group(['prefix' => 'admin'], function () {
Route::get('login', function () {
return view('admin.pages.admin-login');
});
Route::group(['middleware' => 'role:admin'], function () {
Route::get('/', function () {
return view('admin.admin-main');
});
});
});
use it like this way:
return Redirect::to('admin');
And note that:
route:Route::get('company', 'CompanyController#show');
controller:
this works fine:
function show(){
return Redirect::to('home');
}
but this not
function show(){
$this->redirectto();
}
function redirectto()
{
return Redirect::to('home');
}
route.php
Route::get('home', ['as' => 'admin_home', 'uses' => 'HomeController#index']);
Route::get('login'['as'=>'admin_login','uses'=>'LoginController#admin_login']);
LoginController.php
use Illuminate\Support\Facades\Redirect;
public function index(){
$User=new User();
if(isset(AUTH::user()->id)){
$User->id=AUTH::user()->id;
$auth_user_role=$User->auth_user_role();
$rl_title=$auth_user_role[0]->rl_title;
if(isset(Auth::user()->id) && isset($rl_title) && $rl_title == 'Admin'){
return view('home.admin',$this->param);
}
else if(isset(Auth::user()->id) && isset($rl_title) && $rl_title == 'Moderator'){
return view('home.moderator',$this->param);
}
else{
return Redirect::route('admin_login');
}
}else{
return Redirect::route('admin_login');
}
}
Views
-> views
-> home
-> admin.blade.php
-> member.blade.php
I needed to do something like this in Auth/LoginController:
protected function authenticated()
{
if(Auth::user()->isRole('admin')) {
return redirect()->intended('/admin');
}
return redirect()->intended('/home');
}
Don't working authentication. I create authentication manually.
My AdminController:
class AdminController extends Controller
{
public function signin() {
return view('admin.signin');
}
public function index(Request $request) {
dd(Auth::check());
if (Auth::check())
return view('admin.index.index', ['login' => Auth::user()->name]);
else
return redirect()->action('AdminController#signin');
}
public function login() {
$data = Input::all();
if (Auth::attempt(['name' => $data['login'], 'password' => $data['password']])) {
return redirect()->intended('/admin');
} else {
return redirect()->intended('/admin/signin');
}
}
public function logout() {
if (Auth::logout() ) {
return Redirect::to('/admin');
}
}
}
My routes.php file:
//GET
Route::get('/', 'IndexController#index');
Route::get('/admin/signin', 'AdminController#signin');
Route::get('/admin', 'AdminController#index');
Route::get('/admin/logout', 'AdminController#logout');
//POST
Route::post('/admin/auth', 'AdminController#login');
dd(Auth::check()); returned false
What I doing wrong?
In Laravel 5.2 you need to define routes using web middleware to make sessions work, so your routes.php file should look like this:
Route::group(['middleware' => ['web']], function () {
//GET
Route::get('/', 'IndexController#index');
Route::get('/admin/signin', 'AdminController#signin');
Route::get('/admin', 'AdminController#index');
Route::get('/admin/logout', 'AdminController#logout');
//POST
Route::post('/admin/auth', 'AdminController#login');
});
I've created a Laravel blog, the routing works in a way that every page has it's own Route::get('params').
This works fine and I can send the specific content to each page as needed.
The problem is that I'm trying to also send the sidebar content but I'm trying to prevent writing the same code over and over like this:
Route::get('/', function()
{
$sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
$posts = Post::orderBy('id', 'DESC')->get();
return View::make('index')->with('sidebarContent', $sidebarContent)
->with('posts', $posts);
});
Route::get('about', function()
{
$sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
return View::make('about')->with('sidebarContent', $sidebarContent);
});
What's the best way to do this? Is the following the way I should approach it?
Route::group(array(), function()
{
$sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
Route::get('/', function($sidebarContent)
{
$posts = Post::orderBy('id', 'DESC')->get();
return View::make('index')->with('posts', $posts)
->with('sidebarContent', $sidebarContent);
});
Route::get('about', function($sidebarContent)
{
return View::make('about')->with('sidebarContent', $sidebarContent);
});
});
I'll do something like this. :)
Route::get('/', array('uses' => 'Controller#index'));
Route::get('user', array('uses' => 'Controller#about'));
class Controller extends BaseController{
protected $sidebarContent;
public function __construct(){
$this->sideBarContent()
}
public function index(){
$posts = Post::orderBy('id', 'DESC')->get();
return View::make('index')->with('sidebarContent', $this->sidebarContent)
->with('posts', $posts);
}
public function about(){
return View::make('about')->with('sidebarContent', $this->sidebarContent);
}
public function sidebarContent(){
$this->sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
}
}