Laravel 4 Redirects aren't working - php

I am attempting to use return Redirect::route('home'); in my UserController, but it doesn't appear to work (displays a blank page). I've tried naming the route, but to no avail.
<?php
class UserController extends BaseController {
public $restful = true;
public function get_new()
{
return View::make('users.new')
->with('title', 'Rentaholics - Register');
}
public function post_create() {
$validation = Users::validate(Input::all());
if($validation -> passes()){
Users::create(array(
'username'=>Input::get('username'),
'password'=>Hash::make(Input::get('password')),
'email'=>Input::get('email')
));
return Redirect::to_route('home')->with('message', 'Thanks for Registering!');
}else{
return Redirect::to('register')->with_errors($validation)->with_input();
}
}
}

Related

Redirect admin panel

Hello I dont know why and where problem, but when i try to register or login or someting its redirect me Admin Panel, what i can do for leave this? can you help me with this problem? what file i need to change?
admin controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Session;
class AdminController extends Controller
{
public function login(Request $request)
{
if($request->isMethod('post')){
$data = $request->input();
if (Auth::attempt(['email'=>$data['email'],'password'=>$data['password'],'admin'=>'1'])) {
Session::put('adminSession',$data['email']);
return redirect('/admin/dashboard');
}else{
return redirect('/admin')->with('flash_message_error', 'Invalid Username or Password');
}
}
return view('admin.admin_login');
}
public function dashboard()
{
if(Session::has('adminSession'))
{
}else{
return redirect('/admin')->with('flash_message_error', 'Please login to access');
}
return view('admin.dashboard');
}
public function settings()
{
return view('admin.settings');
}
public function logout()
{
Session::flush();
return redirect('/admin')->with('flash_message_success', 'Logged out Successfully');
}
}

How to return response in laravel from trait?

I have controller:
class UserController extends Controller
{
public function index(){
return '1';
}
}
now I want return code from trait like:
class UserController extends Controller
{
use SomeTrait;
public function index(){
$this->traitMethod();
return 2;
}
}
trait SomeTrait
{
public function traitMethod(){
if($this->something == 1){
return '1';
}else{
View::share('somethingElse', 2);
}
}
}
In UserController if $something = 1, trait should return 1 and rest of UserController should't be executed, how can I achieve this?
This this:
class UserController extends Controller
{
use SomeTrait;
public function index(){
$this->traitMethod() ? : return 2;
// if you don't want to return 2, you may return null or something else
}
return 2;
}
}
trait SomeTrait
{
public function traitMethod(){
if($this->something == 1){
return false;
}else{
View::share('somethingElse', 2);
}
}
}

How to get value from table and display it at view?

I'm new to PHP framework (using Yii framework). How to get a value in MySQL then display it at a view? I'm confused in defining a model, controller, and how to use it.
Model:
public $name;
public $info;
$product = product::find()->orderBy('name')->all();
public function tablename()
{
return 'productdata';
}
Controller:
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
public function LoadModel($id){
$model=productdata::model()->find($id);
return $model;
}
Have you tried any tutorials?
[models/MyModel.php]
class MyModel extends CActiveRecord {
public function rules() {
return ['id, name, value', 'safe'];
}
}
[controllers/MyController.php]
class MyController extends CController {
public function actionView($id) {
$model = MyModel::model()->findByPk($id);
$attributes = Yii::app()->request->getParam('MyModel');
if ($attributes) {
if ($model->save()) {
$this->redirect('myController/admin');
} else {
throw new CHttpException(500, 'Model not saved. Use echo CActiveForm::validat($model);');
}
}
$this->render('view', ['model' => $model]);
}
}
[views/MyController/view.php]
<?php echo $model->name; ?>

In Laravel how can I replace a controller/method/id with just /slug in the URL?

What is the best way I could replace /controller/method/id in the URL with just /slug?
For example: trips/1 would become /honduras-trip
This is what I am doing but couldn't their be a better way?
My routes:
Route::get('/{slug}', 'HomeController#show');
My Controller:
public function show($slug)
{
$class = Slug::where('name', '=', $slug)->firstOrFail();
if($class->slugable_type == 'Trip')
{
$trip = Trip::find($class->slugable_id);
return $trip;
}
if($class->slugable_type == 'Project')
{
$project = Project::find($class->slugable_id);
return $project;
}
if($class->slugable_type == 'User')
{
$user = User::find($class->slugable_id);
return $user;
}
}
My Slug Model:
class Slug extends Eloquent {
public function slugable()
{
return $this->morphTo();
}
}
The other models all have this method:
public function slugs()
{
return $this->morphMany('Slug', 'slugable');
}
In your routes.php just give
Route::get('slug', array('uses' => 'HomeController#show'));
In your controller, write show() function
public function show() {
return View::make('welcome');
}
In your view give,
<li>slug</li>

Laravel 4: Pass validation messages obtained from repository to controller

Learning about Ioc and Repositories and stuck at last hurdle!
Assuming I am validating input, how do I pass back messages from the Validator within the repository to the controller?
UserRepository
interface UserRepository {
public function all();
public function create($input);
public function findById($id);
}
Sentry2UserRepository
class Sentry2UserRepository implements UserRepository {
...
public function create($input) {
$validation = Validator::make($input, User::$rules);
if ($validation->passes()) {
Sentry::createUser( array_except( $input, ['password_confirmation']));
// Put something here to tell controller that user has been successfully been created
return true;
}
else {
// pass back to controller that validation has failed
// with messages
return $validation->messages(); ?????
}
...
My UserController
UserController extends BaseController {
...
public function postRegister() {
$input['first_name'] = Input::get('first_name');
$input['last_name'] = Input::get('last_name');
$input['email'] = Input::get('email');
$input['password'] = Input::get('password');
$input['password_confirmation'] = Input::get('password_confirmation');
// Something like
if ($this->user->create($input)) {
Session::flash('success', 'Successfully registered');
return Redirect::to('/');
}
else {
Session::flash('error', 'There were errors in your submission');
return Redirect::to('user/login')->withErrors()->withInput();
}
}
...
}
Only 1.5 weeks into Laravel so please go easy on me.
Assuming your repository is working fine for you already:
class Sentry2UserRepository implements UserRepository {
public $validation;
public function create($input) {
$this->validation = Validator::make($input, User::$rules);
if ($this->validation->passes()) {
Sentry::createUser( array_except( $input, ['password_confirmation']));
// Put something here to tell controller that user has been successfully been created
return true;
}
else {
// pass back to controller that validation has failed
// with messages
return false;
}
}
}
Then you just have to access it within your controller using
$this->user->validation->messages()

Categories