Laravel Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9|Last
Lessons:-Using Controllers

1 <?php
2
3 // application/controllers/account.php
4 public function action_welcome($name, $place)
5 {
6 return View::make('welcome');
7 }
The more nerdy types among my readers will have realized that the statement is telling Laravel
to create (make) a View object from the file application/views/welcome.php (extension not
needed here) and return it as the result of the welcome action.
You will also notice that the make() method looks in the application/views directory for its
views. If you would like to specify an absolute path to a view file simply use the path: prefix,
for example path: /path/to/my/view.php.
Now if we visit /account/welcome/Dayle/Wales we will be greeted with the web page which
we defined in our View file.
Note that you can also use the same sub-directory and bundle prefixes that we previously used
with controllers, to refer to Views.
I know what you’re thinking, now our welcome message isn’t very dynamic at all? Let’s see if
we can fix this. Let’s pass our action parameters to the View. We can do this using the with()
method and we can see Laravel’s elegant method chaining in action, here we go!
1 <?php
2
3 // application/controllers/account.php
4 public function action_welcome($name, $place)
5 {
6 return View::make('welcome')
7 ->with('name', $name)
8 ->with('place', $place);
9 }
Using the with() method we can pass any value (or object) to the View and give it a ‘nickname’
for accessing it from the view. We have used the same nicknames as our parameter names in
this example, but you can call them anything you want!
Now let’s use this data in our view :
1 <h1>Holla!</h1>
2 <p>Welcome to <?php echo $place; ?>, <?php echo $name; ?>!</p>
Now our action works as it did before, only better formatted with neater source code separating
all logic from our visual layer.
Instead of using several with() methods, you can pass an array as a second parameter to make()
with key-value pairs. This can save space but has the same result, here is an example.
Using Controllers 12
1 <?php
2
3 // application/controllers/account.php
4 public function action_welcome($name, $place)
5 {
6 $data = array(
7 'name' => $name,
8 'place' => $place
9 );
10
11 return View::make('welcome', $data);
12 }
Note : I like to call my view array $data, but you can call it whatever you want!
In a later chapter we will cover Views in more detail, including Blade templating, nested views
and other advanced templating options.

 
 
 
image
Anil  Bist

Skills    Laravel

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)