am trying to develop a web application with the help of LARAVEL framework and am successfully installed the Laravel in my laptop.
I want to make a basic controller and a view program. and routing . is there any error in my program and reply to this question please.
My Controller,view, routes files are described in below
NewController.php
<?php
class New_Controller extends BaseController {
public function action_index()()
{
return View::make('hai');
}
}
hai.php
Laravel Basics
<body>
<h1>Jishad is Developing Laravel 4</h1>
</body>
</html>
Routes.php
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
Route::get('/', function()
{
return View::make('hai');
});
You may try something like following. Declare the route (It'll call index method from NewController when you access home page):
Route::get('/', 'NewController#index');
Now create your NewController like this:
// NewController.php
class NewController extends BaseController {
// You may keep this line in your BaseController
// so you don't need to use it in every controller
protected $layout = 'layouts.master';
public function index()
{
// Make the view and pass a $name variable to the view with
// Jishad as it's value and then set the $view to the layout
$view = View::make('hai')->with('name', 'Jishad');
$this->layout->content = $view;
}
}
Now Since you are new to this framework so I would suggest to use controller layout instead of blade layout but you may find everything about layout/templating here. To make it working you need to create the master layout in app/views/layouts folder like this:
// app/views/layouts/master.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simple Web Page</title>
</head>
<body>
<div><?php echo $content; ?></div>
</body>
</html>
Also need to create the hai view in app/views folder like:
// hai.php
<h1>Welcome TO Laravel</h1>
<p><?php echo $name ?> is developing learning Laravel</p>
You need to read more about Laravel, check the Laravel - 4 documentation and read some articles/books. Also You used action_index but it was used in Laravel - 3, just use index.
1.Your routing is wrong, if you want to point your route to a controller do this:
Route::get('/', 'NewController#action_index');
2.If the name of your controller is NewController then your class should also be that:
class NewController extends BaseController {
public function action_index()
{
return View::make('hai');
}
}
3.Also the public function action_index()() should be public function action_index().
Related
In my app I have a users table and a profiles table. When a user goes to their dashboard, they should be able to click a link to view their profile page. Here's the link:
link to your profile page
However, I am getting the error: Route [profiles.show] not defined.
I'm a novice and am not clear on how to link a signed up user with his/her profile page. All users should have a profile page on sign up.
I'd appreciate some guidance! Here is what I have so far:
The link to profile page
link to your profile page
ProfilesController.php
namespace App\Http\Controllers;
use App\Profile;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function show($id)
{
$profile = Profile::find($id);
return view('profiles.show', compact('profile'));
}
}
Profile.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('User');
}
}
routes/web.php
Route::get('pages/profiles', 'ProfilesController#show');
profiles.blade.php
This is just a very simple page for now.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>{{ $user->user_id }}</h1>
<p>{{ $user->about_me }}</p>
</body>
</html>
Solution
I found an easy solution and I wanted to post it here to help others who might be struggling with creating a user profile page. The below assumes you already have a users table in your database and now you want to create a profiles table and connect user ID to their profile page.
Adding Laravel User Profiles
This is the video which help me.
Create table
php artisan make:migration create_profiles_table
This creates a migration file:
2019_09_22_213316_create_profiles_table
Open migration file and add extra columns you need:
$table->integer('user_id')->unsigned()->nullable();
$table->string('about_me')->nullable();
Migrate these to database
php artisan migrate
Now we have our database sorted, we need to create a controller to control how our php functions.
ProfilesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function show($user_id)
{
$user = User::find(1);
$user_profile = Profile::info($user_id)->first();
return view('profiles.show', compact('profile', 'user'));
}
public function profile()
{
return $this->hasOne('Profile');
}
}
routes/web.php
Route::get('dashboard/profile', 'ProfilesController#show');
Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('User');
}
}
Add this to User.php
public function profile()
{
return $this->hasOne('Profile');
}
profile.blade.php
Create any design you want. If you want to pull in users name, include {{ Auth::user()->name }}
try this
Route::get('pages/profiles/{id}', 'ProfilesController#show')->name('profiles.show');
Because in link you use a name route but in web.php there is no name route called profiles.show.so it's show error.And In Route You Need To pass the ID.
At Blade File :
link to your profile page
Change Route Style like below :
Route::get('pages/profiles/{id}', 'ProfilesController#show')->name('profiles.show');
At Profile Model
public function user()
{
return $this->belongsTo(User::class);
}
In Profiles.blade.php
<body>
<h1>{{ $profile->id }}</h1>
<p>{{ $profile->about_me }}</p>
</body>
You Passed User information through "profile" parameter. So you have to write this name in blade file.
Note Route Function won't work if you don't mentioned any name for this Route name. Either you have to use URL.
Your route is wrong, take a look at named routes. If you don't use Route::resource(), you have to manually name your routes and specify when you are expecting a parameter (in this case the profile ID).
Route::get('pages/profiles/{id}', 'ProfilesController#show')->name('profiles.show');
link to your profile page
Route model binding is probably the way to go in this case.
Route::get('pages/profiles/{profile}', 'ProfilesController#show')->name('profiles.show');
namespace App\Http\Controllers;
use App\Profile;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function show(Profile $profile)
{
return view('profiles.show', compact('profile'));
}
}
hi I have created a new default index view but it not loading its content and showing the blank page,,, my file structure https://ibb.co/wW337xS
index blade:
#extends('layouts.frontLayout.front_design')
#section('content')
<!--html here-->
#endsection
controller:
<?php
namespace App\Http\Controllers;
use App\Index;
use Illuminate\Http\Request;
class IndexController extends Controller
{
public function index()
{
return view('index');
}
route:
Route::resource('/','IndexController');
Its problem is occured, becouse you send user to the view with name of 'index', it means that there is a blade page in the view folder of your resource, but as i see in your structure, the index.blade.php is in this address: layouts.index. then you can refactor your controller:
public function index()
{
return view('layouts.index');
}
wrong
Route::resource('/','IndexController');
right
Route::get('/', 'IndexController#index');
Trying to pass a data array to all views, but I keep getting the error Undefined variable: data. Not sure what the missing piece is.
All my page specifric controllers extend the following BaseController
BaseController
<?php
...
class BaseController extends Controller
{
public function theme_options() {
// Set number of columns
$footer_cols = DB::table('theme_settings')->where('id','=','200')->value('setting_value');
$widget_width = 12 / $footer_cols;
// Footer Settings
$data = array(
'widget_width' => $widget_width
);
return $data;
}
}
layouts/default.blade.php
<!doctype html>
<html class="no-js" lang="">
<head>
#include('blocks.head')
</head>
<body>
#include('blocks.header')
#yield('content')
#include('blocks.scripts')
#include('blocks.footer', ['data' => $data])
</body>
</html>
Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method.
In your AppServiceProvider boot method add the following code
// Set number of columns
$footer_cols = DB::table('theme_settings')
->where('id', '=', '200')
->value('setting_value');
$widget_width = 12 / $footer_cols;
View::share('widget_width', $widget_width);
so that the variable widget_width is available to all your views.
Here is the Documentation
P.S: Make sure to do the necessary imports like
use Illuminate\Support\Facades\View;
use DB;
You need to return those variable to view (blade) file try the below code :
class BaseController extends Controller
{
public function theme_options() {
// Set number of columns
$footer_cols = DB::table('theme_settings')->where('id','=','200')->value('setting_value');
$widget_width = 12 / $footer_cols;
// Footer Settings
$data = array(
'widget_width' => $widget_width
);
return view('your_view_name')->with($data);
}
}
Create a new provider for data sharing with views
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider
{
public function boot()
{
$footer_cols = DB::table('theme_settings')->where('id','=','200')->value('setting_value');
return view()->composer('*',function($view){
$view->with('pages',12 / $footer_cols);
});
}
public function register()
{
}
}
Don't forget to add ViewComposerServiceProvider to providers array in config/app.php file
I'm starting to use Laravel 5.1 from 4.2 and I have a question about the definition of layouts in the controller.
In 4.2 I have this:
private $layout = 'layouts.master';
public function showWelcome()
{
$this->layout->content = View::make('home');
}
When the view is loaded, the "Home" view will appear in the #yield('section') of "master.blade.php" in the layouts folder.
I searched for how to use this in 5.1 and I see that the assignment of the layout.masters has been removed, but I can't see the new usage anywhere.
Now in 5.1 I have:
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use View;
class HomeController extends BaseController
{
public function showWelcome()
{
return view('home');
}
}
How can I say to the showWelcome() function that it has to yield the content with the view?
In Laravel 5.1 you can extend master layout in blade files writing at the top #extends('layouts.master') . From Laravel 5.1 Documentation
<!-- Stored in resources/views/child.blade.php -->
#extends('layouts.master')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<p>This is my body content.</p>
#endsection
Struggling to figure out how to achieve this. I want a login/registerform on same page when using tank_auth in CodeIgniter.
I was thinking of having something like this in my members controller (which extends My_Controller which extends Auth).
class Members extends My_Controller (
public function login()
{
//Already loggedin, do nothing
if ($this->tank_auth->is_logged_in()) {
return;
}
$this->view_data['login_by_username'] = true;
$this->view_data['login_by_email'] = false;
$this->view_data['show_captcha'] = false;
$this->view_data['login_form'] = $this->load->view('auth/login_form', $this->view_data, TRUE);
$this->view_data['register_form'] = $this->load->view('auth/register_form', $this->view_data, TRUE);
$this->v('members/login_register'); //In this view $login_form and $register_form is rendered
}
}
When I go to that page (localhost/members/login) the members/login-view is shown as expected, but in the action of the form for loginform it says:
http://localhost/members/login
Therefore when I click "Login-button" then it just calls members/login and that's not what I want. I want the action to be auth/login, but I still want to use my own template.
I hope you guys understand what I mean.... Please tell me if I'm doing anything wrong/thinking of this incorrectly.
UPATE:
In the actual template (members/login_register) it looks like this:
<div class="column">
<?php echo $login_form;?>
</div>
<div class="column>
<?php echo $register_form;?>
</div>
Maybe what I want to achieve is not possible?
Master View
You should use a Master view as a wrapper for your content.
That way you can easily pass in partial views(although not required)
However it will keep things neat. It also allows for easier control over
Admin/User Dashboards and your frontend.
Main Controller
class MY_Controller extends CI_Controller
{
public $template;
public function __construct()
{
$this->template = "master/template"; //views/master/template
}
}
To change it for an admin template(as an example of flexibility) you simply need to change it
the variable in the __constructor
class Admin_Controller extends MY_Controller
{
public function __construct()
{
$this->template = "master/admin/template"; //views/master/admin/template
}
}
Partial View(s)
Partial views can be used directly in other views.
You don't need a controller to call them.
These views are just stored in the buffer when they are
loaded by the loader class $this->load->view('', '', true)
A common approach is to create a folder inside /views called "partials".
This is where you would keep all you re-usable views(such as forms/widgets).
The Views(/views/partials/login_form)
<div>
<?php echo form_open('members/login', array('id'=>'login-form'))
</div>
The Views(/views/partials/signup_form)
<div>
<?php echo form_open('members/signup', array('id'=>'signup-form'))
</div>
The Views(members/index)
You can then combine the views
<div class="signup-form">
<?php echo $this->load->view('partials/signup_form')
</div>
<div class="login-form">
<?php echo $this->load->view('partials/login_form')
</div>
Login / Signup
In your members class you can create one method to show
the register/login form and then create methods to handle each of them individually.
class Members extends MY_Controller
{
public function index()
{
return $this->load->view('members/login_register');
}
public function login()
{
if(!$this->form_validation->run()){
return $this->index();
}
// form validation passed Ask Tank Auth
// to log the user in
$tank->auth->login();
}
public function signup()
{
if(!$this->form_validation->run()){
return $this->index();
}
// form validation passed Ask Tank Auth
// to register
$tank->auth->register();
}
}