Custom URL routing including identifier/controller/method - php

I'm trying to create a bug tracking software with multiple projects support.
To achieve that I'd like to use a URL like this:
http://example.com/project/default_project/tickets/view/123
project is the Project-Controller
default_project is in this case the project identifier.
tickets is the controller
view is the method in the tickets-controller
123 is the ID of the ticket passed to view
How do I create such a routing?
My controller would look like this:
class Tickets extends CI_Controller {
public function index() {
// load all tickets WHERE project_identifier = $this->uri->segment(1)
}
public function view($id) {
// load ticket WHERE project_identifier = $this->uri->segment(n)
// AND ticket_id = $id
}
}

I fixed my problem by adding this line to routes.php in the config folder:
$route['project/(:any)/ticket/(:num)'] = 'tickets/view/$1/$2';

Related

How to show sub router?

Suppose I have this controller:
class Site extends CI_Controller {
public function __constructor()
{
parent::__constructor();
echo 'test';
}
public function index()
{
$view['site'] = true;
$view['view_home'] = lang('home');
$view['view_home_url'] = base_url();
$view['view_name'] = lang('home');
$view['content'] = 'site/home';
$this->load->view('partials/template', $view);
}
public function association()
{
echo 'test';
}
}
when I type: http://localhost/mysite/association I get 404. Essentially I would like to use the same controller Site which is the default controller, to load multiple routers. How can I do this?
You are using codeigniter and you can't access url lik below as you haven't defined any routes for that. http://localhost/mysite/association . You will have to go url like
http://localhost/mysite/site/association //sometimes need index.php
if you don't configure it. first parameter is the controller and second one is a method
For going to your custom URL you will have to go to application/config/routes.php and define your new custom
route $route['association'] = 'site/association';
You can also define like below routes and access from browser
$route['association/url1/url2/url2'] = 'site/association'; //simple
Hope that you have got your answer

Include different variables in some views passing through controller

I'm new of Laravel and I have started my first project (with Laravel 5.7).
I have some variables that I would like to use in every single view.
In general I create, for example, a config.php file where I put my variables and use them in every pages (obviusly including config.php in all pages).
But, with Laravel, where can I put this variables? And how can I do to use them in all views?
This is my web.php:
Route::get('/task','TaskController#index');
Route::get('/task/insert','TaskController#setInsertTask');
Route::get('/task/list','TaskController#getTaskList');
And in the TaskController:
class TaskController extends Controller
{
public function index(){
return view('task.index');
}
public function setInsertTask(){
return view('task.insert');
}
public function getTaskList(){
return view('task.list');
}
}
Now I have tried to put the variables in the TaskController like this:
class TicketController extends Controller
{
private $titlePage1 = "Task manager";
private $titlePage2 = "Task manager insert";
public function index(){
return view('task.index',[
'titlePage' => $this->titlePage1
]);
}
public function setInsertTask(){
return view('task.insert',[
'titlePage' => $this->titlePage2
]);
}
public function getTaskList(){
return view('task.list',[
'titlePage' => $this->titlePage1
]);
}
}
And in the view I have insert something like this:
#extends('layout.layout')
#section('content')
<h1>{{ $titlePage }}</h1>
#endsection
But I don't think that is the best solution and I don't like it.
In this project I would like to use this variable beacuse:
1. I would like to managed three different software related each other (ex. Login for users, login for admin, login for technicians) thet they have the same database and the single area is small. So, for each area I'll liked to print a different title.
2. In the pages there are some static word, so I will create an array with all words in such a way to concenter all static words.
3. Like the title page, I would like the same things with a menu. Different menus for different areas managed in a single file in php (not in the html).
4. The same variables I will like to use them in other controllers.
I have searched a lot but I can't find which is the best practise to include a general variable in some views.
Can anyone help me? Thanks a lot.
You can share variables for all views with View::share in AppServiceProvider
I had answered in another question. For details visit this: link
Yes, you can use the variables defined in the .env at route
for example in .env
name=test
You can get it as env('name')
read here
You can do it using BaseController
class BaseController extends Controller
{
public function __construct()
{
$titlePage1 = "Task manager";
$titlePage2 = "Task manager insert";
View::share(['titlePage1' => $titlePage1, 'titlePage2' => $titlePage2 ]);
}
}
You can access it in any view {{$titlePage1}} and {{$titlePage2}}
You can also perform same thing with AppServiceProvider
In boot() of AppServiceProvider, add following code.
public function boot() {
$titlePage1 = "Task manager";
$titlePage2 = "Task manager insert";
View::share(['titlePage1' => $titlePage1, 'titlePage2' => $titlePage2 ]);
}

Codeigniter HMVC pass data from module to controller

I'm building an application in Codigniter with HMVC. In it I call a module inside another controller. The problem I've run into is trying to pass/retrieve the data loaded into module. Specifically, I'm loading some javascript files that I would then like to pass to the calling controller.
Here is a simplified code:
public function module()
{
...
$this->data['js'] = $this->js_assets;
...
return $this->load->view('module_view', $this->data, true);
}
public function controller()
{
...
$this->load->module('module/module');
$this->data['module'] = $this->module->module();
...
}
I know that I can retrieve data['js'] in module_view as $js, but I wonder if I can just pass the data directly to the controller.
What I'd like to do is get data from the module to the calling controller
Yes, you can do that with HMVC.
In the calling module you can organise code like this (pseudocode):
class Someclass extends CI_Controller {
...
public function show_page()
{
$this->load->module("module");
$js_files = $this->module->get_js_files();
$this->load->view("header", array("js_files" => $js_files));
}
...
}
In the callee module you would write something along these lines:
class Module extends CI_Controller {
...
public function get_js_files()
{
$scripts = $this->frontend_model->get_scripts();
return $scripts;
}
....
}
(Although in this fantasy case it would be wiser to get data from model in the first place)
Another technique is, like #wolfgang1983 correctly mentioned, to call another module like this:
Modules::run('modulename/controller/function', $data);
and in order to get data from that controller you just have to assign returned data to a variable:
$data_received = Modules::run('modulename/controller/function', $data);
But in this case you can't get variables, only buffered output (like loaded and processed view files or echoed statements).

Share 2 data to all view in laravel 4.2

I want to share this data to all views with laravel 4.2
But, can I use array to pass this 2 data with View::share()?
class Sidebar extends BaseController {
public function __construct() {
$package_sidebar = TravelPackage::orderBy('idTravelPackage','DESC')->take(4)->get();
$artikel_sidebar = Artikel::orderBy('date','DESC')->take(4)->get();
View::share()
}
}
Put this in your routes.php or somewhere else more appropriate:
<?php
View::share('package_sidebar', TravelPackage::orderBy('idTravelPackage','DESC')->take(4)->get());
View::share('artikel_sidebar', Artikel::orderBy('date','DESC')->take(4)->get());
Then you'll be able to reference $package_sidebar and $artikel_sidebar in any view.

URL segment - User Profiles - Codeigniter

I'm trying to create user profiles for my site where the URL is something like
mysite.com/user/ChrisSalij
Currently my controller looks like so:
<?php
class User extends Controller {
function user(){
parent::Controller();
}
function index(){
$data['username'] = $this->uri->segment(2);
if($data['username'] == FALSE) {
/*load default profile page*/
} else {
/*access the database and get info for $data['username']*/
/*Load profile page with said info*/
}//End of Else
}//End of function
}//End of Class
?>
At the moment I'm getting a 404 error whenever i go to;
mysite.com/user/ChrisSalij
I think this is because it is expecting there to be a method called ChrisSalij.
Though I'm sure I'm misusing the $this->uri->segment(); command too
Any help would be appreciated.
Thanks
It's because the controller is looking for a function called ChrisSalij.
A few ways to solve this:
change
function index(){
$data['username'] = $this->uri->segment(2);
to be
function index($username){
$data['username'] = $username;
and use the URL of mysite.com/user/index/ChrisSalij
if you don't like the idea of the index being in the URL you could change the function to be called a profile or the like, or look into using routing
and use something along the lines of
$route['user/(:any)'] = "user/index/$1";
to correctly map the URL of mysite.com/user/ChrisSalij

Categories