how to move some controllers and views to subfolder? - laravel

I have old project that I want to move it is controllers and views to my new project bu copy and past them, the controller will be pasted in subfolder Http\mod1 and views into Resources\views\mod1, also I will copy the web.php content of the old project and past it into my new project using route grouping for it is routes.
Now since there is a lot of views and controller is there is some way to instruct Laravel that the view that are called by the controllers under group are located under sub folder mod1 ? and how to make the route group point to the controllers inside that group without modifying them one by one?

You can achieve it by doing as below.
Move your View file in mod1 subfolder
// In your Controller
namespace App\Http\Controllers\mod1;
...
return view('mod1/index'); // to return your view inside a method for example.
// In Your route for example you can goup all your controllers in namespace
Route::group([ 'namespace' => 'mod1'] ,function() {
Route::GET('/user', 'UserController#getuser');
...
});

Related

laravel send variable to login route

I am using a different app.blade.php than the one provided with the default auth system in laravel. In the one I have created, I need to give a variable each time so that it show where we are in the code. For example, for my routes, I use:
Route::get('/', ['as' => 'home', function () {
$position = ['main_title'=>'Home','second_title'=>'',
'urls'=>
[
['name'=>'home','url'=>'#']
]
];
return view('home')->with('position',$position);
}]);
And in my app.blade.php, I can use this $position to print the main title etc.
Now the problem comes when I try to access login because $position is not set. How can I achieve that in Laravel so that the auth system will use my new blade system?
Thanks.
The auth views use the layout file app.blade.php located in resources/views/layouts. This is done by the code #extends('layouts.app') at top of all the views, which means that the auth views extend this layout.
To change the layout in all the auth views, manually replace the #extends('layouts.app') part at top with the new layout file you have created. For example if your new custom layout file is resources/views/base.blade.php, then replace the default extends to #extends('base').
If you want to use a variable in different (all) views, you may use ViewComposer

Laravel include view

I want to include a view like so: #include(user.myview), but within this view I need UserController logic. So I thought about calling a route: #include( route('user.route') ) which calls a Controllerfunction and returns the view but that isn't working. Any Ideas how to deal with this problem?
You need to create view composer and use it to get the data.
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.
Simply add a link in you view and include it in your desired location.
Link will have a route.
On clicking the link, controller method can be called. e.g. show_link.blade.php
In your show_link.blade.php view:
<a href= {{route('route-name')}} > Click here</a>.
This route will call a method via .
Route::get('/call/method', 'controller#your_method_name')->name('route-name');
In controller, method your_method_name that will look like this:
public function your_method_name()
{
return "show what you want to";
}

Laravel 3: View doesn't exist

I've just inherited a Laravel 3 site which works on a custom CMS. The CMS output is rendered through a theme folder at the / level so my folder structure looks like:
-application
-bundles
-laravel
-public
-storage
-theme
-errors
-layouts
-partials
I've made a search controller within '/application/controllers' and I want to create my view for the output in the '/theme/layouts' folder with the other template files. When I've worked with Laravel before, my views are all within '/application/views' and I can specify my view with:
public $layout = 'layouts.default';
..which would use '/application/views/layouts/default.blade.php'
How can I get my controller to render the view using my '/theme/layouts/searchTemplate.php' file and pass in the search data from the controller?
If you really need to put these files in a separate folder you should probably use bundles (see docs).
However, a quick and dirty solution is to add a hook in the view loader event (application/start.php):
Event::listen(View::loader, function($bundle, $view)
{
if($bundle == 'theme') {
return View::file('application', $view, Bundle::path('application').'theme');
}
return View::file($bundle, $view, Bundle::path($bundle).'views');
});
You can then make views like:
View::make('theme::layouts.default');
which will load the file "application/theme/layouts/default.blade.php".
In your application folder, there is a folder called 'views'
You have to create the default.php in applications/views/layouts/
With 'layouts.defaults' it search in the views folder to a folder that will be called layouts.
So the complete folder for 'layouts.default' would be: ./application/views/layouts/default.php
Edit: answer is not relative anymore

Laravel 4 passing data to #include

I have a view called admin.users. In that view I include header and footer using #include directive. I write all my views-including stuff in routes.php, so for admin.users it is:
Route::get('users', function() {
// ...
return View::make('admin.users')->with('num', $usersNum);
}
And in users.blade.php:
#include('admin.partials.header')
// ....
#include('admin.partials.footer')
Is it possible to pass "users num" to header view in order to show that variable? And is it a good practice the way I'm combining views, because I read about controller layouts but actually I decided to have only rest controllers while I include view only in routes.php (like load static markup and after that communicate with server by ajax)
Have a look at 'ViewComposers' (http://laravel.com/docs/responses#view-composers). This is a great way to share data with your views and keep your routes file clean.
In global.php (or any other place really) add:
View::composer('admin.users', function($view)
{
// Do your $usersNum logic here
...
$view->with('num', $usersNum);
});
If at some point you want this data to be available in admin.dashboard as well, just rewrite to:
View::composer(array('admin.users', 'admin.dashboard'), function($view)
As I said in my comment (and what seems to have fixed your issue) you should create an admin.template view with your header and footer in it, and start your views with #extends('admin.template').

codeigniter:everything created through MVC pattern?

I'm studying codeigniter and I would realize a simple application. I'm asking if every page, even if doesn't not contain directly dynamic element must be create through MVC pattern? I explain myself: my home page will not contain anything of dinamic. only an header, menu and footer. it needs to create model,controller and view to handle this situation or I create simple the home page?
You always have to create a controller because that is what is called from the url.
As far as the view and model. You don't always have to create either.
I've got plenty of pages with static info so I don't need any model interaction at all.
Without a view you are kind of defeating the purpose of the MVC. It is possible for the controller to just echo all your html for the page but I wouldn't do it.
The way I do it is that I have a default view that contains the header and footer. A content view that all my content for the page goes into. I then pump my view for the page into the content view then that into the default view to create my page.
$arrData["vwsContent"] = $this->load->view("your view for the page", $arrData, TRUE);
$arrData["vwsPageContent"] = $this->load->view("content template view", $arrData, TRUE);
$this->load->view("default template view", $arrData, FALSE);
In this way I can have different content views but the same default view for all the pages. For instance my homepage looks different than my regular pages so I would have a HOME template to use instead of a CONTENT template.
You can define the home page function in any controller.
In routes.php the default controller and action can be defined
$route['default_controller'] = "welcome"; (welcome can be replaced by any your prefer controller) .
Create function with name index
function index(){
$this->load->view('index');
}
Then create the file index.php in "views" folder.
In index.php you can put all your HTML static content. You can use URL helper [ function base_url()] for images/css/js path.

Resources